diff --git a/ConsoleRayTracing.cpp b/ConsoleRayTracing.cpp index f984f19..2eab22f 100644 --- a/ConsoleRayTracing.cpp +++ b/ConsoleRayTracing.cpp @@ -1,38 +1,27 @@ #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); -} +#include "VecFunctions.h" +#include "platforms.cpp" int main() { - int width = 120 * 2; - int height = 30 * 2; - SetWindow(width, height); + + std::ios_base::sync_with_stdio(false); + std::cin.tie(NULL); + + clearScreen(); + 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 +68,15 @@ 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.flush(); + gotoxy(0,0); + } -} \ No newline at end of file +} diff --git a/platforms.cpp b/platforms.cpp new file mode 100644 index 0000000..65e7456 --- /dev/null +++ b/platforms.cpp @@ -0,0 +1,49 @@ +#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"; + gotoxy(0,0); +} + +#endif