Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added *nix support #3

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 25 additions & 31 deletions ConsoleRayTracing.cpp
Original file line number Diff line number Diff line change
@@ -1,38 +1,27 @@
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <math.h>
#include <windows.h>
#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 <std::string> 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);
Expand Down Expand Up @@ -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);

}
}
}
49 changes: 49 additions & 0 deletions platforms.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <iostream>

int width = 120 * 2, height = 30 * 2;

#if defined(_WIN32) || defined(_WIN64)

// Windows
#include <windows.h>
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 <sys/ioctl.h>
#include <unistd.h>

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