From 589d0ec95699adf312be5db23034724180fb74f9 Mon Sep 17 00:00:00 2001 From: Lrapava <46052668+Lrapava@users.noreply.github.com> Date: Sat, 27 Nov 2021 15:29:51 +0400 Subject: [PATCH 1/6] Added *nix support Added support for Unix-like operating systems (Unix/Linux/MacOS) support. The code works fine on Linux. ATTENTION: I have NOT tested the code on Windows, BSD and/or MacOS yet. Please test before accepting the changes. The program should automatically detect the size of the terminal terminal and set width and height accordingly. + a few small changes. --- ConsoleRayTracing.cpp | 78 +++++++++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 29 deletions(-) diff --git a/ConsoleRayTracing.cpp b/ConsoleRayTracing.cpp index f984f19..22f3472 100644 --- a/ConsoleRayTracing.cpp +++ b/ConsoleRayTracing.cpp @@ -1,38 +1,54 @@ #include -#include +#include +#include #include -#include #include "VecFunctions.h" -void SetWindow(int Width, int Height) -{ - _COORD coord; - coord.X = Width; - coord.Y = Height; - _SMALL_RECT Rect; - Rect.Top = 0; - Rect.Left = 0; - Rect.Bottom = Height - 1; - Rect.Right = Width - 1; - HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE); - SetConsoleScreenBufferSize(Handle, coord); - SetConsoleWindowInfo(Handle, TRUE, &Rect); +int width = 120 * 2, height = 30 * 2; + +#if defined(_WIN32) || defined(_WIN64) + +// Windows +#include +int findWindowSize(int argc, char *argv[]) { + CONSOLE_SCREEN_BUFFER_INFO csbi; + + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); + width = csbi.srWindow.Right - csbi.srWindow.Left + 1; + height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; +} + +#else + +// *nix +#include +#include +#include + +void findWindowSize() { + struct winsize w; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); + height = w.ws_row - 1; + width = w.ws_col; } +#endif + int main() { - int width = 120 * 2; - int height = 30 * 2; - SetWindow(width, height); + + std::ios_base::sync_with_stdio(false); + std::cin.tie(NULL); + + findWindowSize(); + float aspect = (float)width / height; float pixelAspect = 11.0f / 24.0f; - char gradient[] = " .:!/r(l1Z4H9W8$@"; - int gradientSize = std::size(gradient) - 2; - - wchar_t* screen = new wchar_t[width * height]; - HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL); - SetConsoleActiveScreenBuffer(hConsole); - DWORD dwBytesWritten = 0; + std::string gradient = " .:!/r(l1Z4H9W8$@"; + int gradientSize = gradient.size() - 1; + std::string str(width, ' '); + std::vector screen(height, str); + for (int t = 0; t < 10000; t++) { vec3 light = norm(vec3(-0.5, 0.5, -1.0)); vec3 spherePos = vec3(0, 3, 0); @@ -79,10 +95,14 @@ int main() { int color = (int)(diff * 20); color = clamp(color, 0, gradientSize); char pixel = gradient[color]; - screen[i + j * width] = pixel; + screen[j][i] = pixel; } } - screen[width * height - 1] = '\0'; - WriteConsoleOutputCharacter(hConsole, screen, width * height, { 0, 0 }, &dwBytesWritten); + + for (int i = 0; i < height; i++) { + std::cout << screen[i] << "\n"; + } + std::cout << "\033[0;0f"; + std::cout.flush(); } -} \ No newline at end of file +} From 9f6061f8a850b38c1c160bd7ac70bd7bbddaae26 Mon Sep 17 00:00:00 2001 From: Lrapava <46052668+Lrapava@users.noreply.github.com> Date: Sat, 27 Nov 2021 19:23:50 +0400 Subject: [PATCH 2/6] Update ConsoleRayTracing.cpp --- ConsoleRayTracing.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ConsoleRayTracing.cpp b/ConsoleRayTracing.cpp index 22f3472..b416997 100644 --- a/ConsoleRayTracing.cpp +++ b/ConsoleRayTracing.cpp @@ -10,7 +10,7 @@ int width = 120 * 2, height = 30 * 2; // Windows #include -int findWindowSize(int argc, char *argv[]) { +int findWindowSize() { CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); @@ -33,7 +33,6 @@ void findWindowSize() { } #endif - int main() { std::ios_base::sync_with_stdio(false); @@ -102,7 +101,14 @@ int main() { for (int i = 0; i < height; i++) { std::cout << screen[i] << "\n"; } - std::cout << "\033[0;0f"; + + #if defined(_WIN32) || defined(_WIN64) + gotoxy(1,1); + #else + std::cout << "\033[0;0f"; + #endif + std::cout.flush(); } } + From 072bb3be50c3dff4f891820235dc7e16d99bc64e Mon Sep 17 00:00:00 2001 From: Lrapava <46052668+Lrapava@users.noreply.github.com> Date: Sat, 27 Nov 2021 21:08:00 +0400 Subject: [PATCH 3/6] Update ConsoleRayTracing.cpp --- ConsoleRayTracing.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ConsoleRayTracing.cpp b/ConsoleRayTracing.cpp index b416997..93a9287 100644 --- a/ConsoleRayTracing.cpp +++ b/ConsoleRayTracing.cpp @@ -10,6 +10,8 @@ int width = 120 * 2, height = 30 * 2; // Windows #include +#include + int findWindowSize() { CONSOLE_SCREEN_BUFFER_INFO csbi; From 65cef92298cb7884ab7b7666a2b1f3d45fd45e14 Mon Sep 17 00:00:00 2001 From: Lrapava <46052668+Lrapava@users.noreply.github.com> Date: Sun, 28 Nov 2021 10:21:55 +0400 Subject: [PATCH 4/6] Add files via upload --- platforms.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 platforms.cpp diff --git a/platforms.cpp b/platforms.cpp new file mode 100644 index 0000000..76c5eb7 --- /dev/null +++ b/platforms.cpp @@ -0,0 +1,48 @@ +#include + +int width = 120 * 2, height = 30 * 2; + +#if defined(_WIN32) || defined(_WIN64) + +// Windows +#include +int findWindowSize() { + CONSOLE_SCREEN_BUFFER_INFO csbi; + + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); + width = csbi.srWindow.Right - csbi.srWindow.Left; + height = csbi.srWindow.Bottom - csbi.srWindow.Top; +} + +void gotoxy(int x, int y) { + COORD p = { x, y }; + SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), p); +} + +void clearScreen() { + system("cls"); +} + +#else + +// *nix +#include +#include + +void findWindowSize() { + struct winsize w; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); + height = w.ws_row - 1; + width = w.ws_col; +} + +void gotoxy(int x, int y) { + x++; y++; + std::cout << "\033[" << y << ";" << x << "f"; +} + +void clearScreen() { + std::cout << "\033[2J"; +} + +#endif From 197491e4904daa2e8097389f4c9db05a578f1f71 Mon Sep 17 00:00:00 2001 From: Lrapava <46052668+Lrapava@users.noreply.github.com> Date: Sun, 28 Nov 2021 10:22:20 +0400 Subject: [PATCH 5/6] Update ConsoleRayTracing.cpp --- ConsoleRayTracing.cpp | 48 +++++++------------------------------------ 1 file changed, 7 insertions(+), 41 deletions(-) diff --git a/ConsoleRayTracing.cpp b/ConsoleRayTracing.cpp index 93a9287..2eab22f 100644 --- a/ConsoleRayTracing.cpp +++ b/ConsoleRayTracing.cpp @@ -2,46 +2,18 @@ #include #include #include -#include "VecFunctions.h" - -int width = 120 * 2, height = 30 * 2; - -#if defined(_WIN32) || defined(_WIN64) - -// Windows -#include -#include - -int findWindowSize() { - CONSOLE_SCREEN_BUFFER_INFO csbi; - GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); - width = csbi.srWindow.Right - csbi.srWindow.Left + 1; - height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; -} - -#else - -// *nix -#include -#include -#include - -void findWindowSize() { - struct winsize w; - ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); - height = w.ws_row - 1; - width = w.ws_col; -} -#endif +#include "VecFunctions.h" +#include "platforms.cpp" int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); - - findWindowSize(); + clearScreen(); + findWindowSize(); + float aspect = (float)width / height; float pixelAspect = 11.0f / 24.0f; std::string gradient = " .:!/r(l1Z4H9W8$@"; @@ -103,14 +75,8 @@ int main() { for (int i = 0; i < height; i++) { std::cout << screen[i] << "\n"; } - - #if defined(_WIN32) || defined(_WIN64) - gotoxy(1,1); - #else - std::cout << "\033[0;0f"; - #endif - std::cout.flush(); + gotoxy(0,0); + } } - From 05058e985f654d9cd3b22dc835bbe78537c9f131 Mon Sep 17 00:00:00 2001 From: Lrapava <46052668+Lrapava@users.noreply.github.com> Date: Sun, 28 Nov 2021 11:52:26 +0400 Subject: [PATCH 6/6] Update platforms.cpp --- platforms.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/platforms.cpp b/platforms.cpp index 76c5eb7..65e7456 100644 --- a/platforms.cpp +++ b/platforms.cpp @@ -43,6 +43,7 @@ void gotoxy(int x, int y) { void clearScreen() { std::cout << "\033[2J"; + gotoxy(0,0); } #endif