Skip to content

Commit

Permalink
Add console size functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
wojciech-graj committed Jan 30, 2022
1 parent 2f8d465 commit 7c7d4bb
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
A terminal-based graphics library for both 2D and 3D graphics.\
Works in all terminals supporting ANSI escape codes.\
Support for Windows and UNIX.\
C11 compliant, with no external dependencies.\
Can read from terminal for user-interaction.
C99 compliant, with no external dependencies.\
Realtime input reading from terminal for user-interaction.

## Table of Contents

Expand Down
14 changes: 11 additions & 3 deletions demodir/termgl_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ typedef struct STLTriangle {
#define xstr(str_) str(str_)
#define str(str_) #str_

const char *HELPTEXT = "TermGL v" xstr(TGL_VERSION_MAJOR) "." xstr(TGL_VERSION_MINOR) "\n\
const char *HELPTEXT_HEADER = "TermGL v" xstr(TGL_VERSION_MAJOR) "." xstr(TGL_VERSION_MINOR) " Demo Utility";
const char *HELPTEXT_BODY = "\
Select a Demo:\n\
1. Utah Teapot\n\
Renders a rotating 3D Utah Teapot.\n\
Expand All @@ -34,7 +35,7 @@ Select a Demo:\n\
3. Mandelbrot\n\
Renders an infinitely zooming-in Mandelbrot set.\n\
4. Realtime Keyboard\n\
Displays keyboard input in realtime.\n\
Displays keyboard input in realtime.\
";

void teapot_intermediate_shader(TGLTriangle *trig, void *data);
Expand Down Expand Up @@ -322,12 +323,19 @@ void demo_color(const unsigned res_x, const unsigned res_y, const unsigned frame
tgl_delete(tgl);
}

#include <windows.h>

int main(int argc, char **argv)
{
(void)argc;
(void)argv;

puts(HELPTEXT);
puts(HELPTEXT_HEADER);
unsigned col, row;
tglutil_get_console_size(&col, &row, true);
printf("Console size: %ux%u\n", col, row);
puts(HELPTEXT_BODY);

unsigned n = 0;
assert(scanf("%u", &n) == 1);

Expand Down
14 changes: 14 additions & 0 deletions lib/termgl.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,20 @@ void tgl3d_transform_apply(TGLTransform *transform, TGLVec3 in[3], TGLVec3 out[3
*/
TGL_SSIZE_T tglutil_read(char *buf, size_t count);

/**
* Stores number of console columns and rows in *col and *row respectively
* @param screen_buffer: true for size of screen buffer, false for size of window. On UNIX, value is ignored and assumed true.
* @return 0 on success, -1 on failure
*/
int tglutil_get_console_size(unsigned *col, unsigned *row, bool screen_buffer);

/**
* Sets console size
* Only changes printable area and will not change window size if new size is larger than window
* @return 0 on success, -1 on failure
*/
int tglutil_set_console_size(unsigned col, unsigned row);

#endif /* TERMGLUTIL */

#ifdef __cplusplus
Expand Down
42 changes: 42 additions & 0 deletions src/termgl.c
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,7 @@ TGLTransform *tgl3d_get_transform(TGL *tgl)

#ifdef __unix__
#include <termios.h>
#include <sys/ioctl.h>
#endif

TGL_SSIZE_T tglutil_read(char *buf, size_t count)
Expand Down Expand Up @@ -1048,4 +1049,45 @@ TGL_SSIZE_T tglutil_read(char *buf, size_t count)
return retval;
}

int tglutil_get_console_size(unsigned *col, unsigned *row, bool screen_buffer)
{
#ifdef __unix__
(void)screen_buffer;
struct winsize w;
int retval = ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
*col = w.ws_col;
*row = w.ws_row;
#else /* defined(TGL_OS_WINDOWS) */
CONSOLE_SCREEN_BUFFER_INFO csbi;
int retval = GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) ? 0 : -1;
if (screen_buffer) {
*col = csbi.dwSize.X;
*row = csbi.dwSize.Y;
} else {
*col = csbi.srWindow.Right - csbi.srWindow.Left + 1;
*row = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
}
#endif
return retval;
}

int tglutil_set_console_size(unsigned col, unsigned row)
{
#ifdef __unix__
struct winsize w = (struct winsize) {
.ws_row = row,
.ws_col = col,
};
int retval = ioctl(STDOUT_FILENO, TIOCSWINSZ, &w);
#else /* defined(TGL_OS_WINDOWS) */
COORD size = (COORD) {
.Y = row,
.X = col,
};
int retval = SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), size) ? 0 : -1;
#endif
return retval;
}


#endif /* TERMGLUTIL */

0 comments on commit 7c7d4bb

Please sign in to comment.