From 07ef81d009a8eec63df7a04b149f6ddea341cef0 Mon Sep 17 00:00:00 2001 From: ashley zomo Date: Fri, 24 Dec 2021 00:28:21 -0600 Subject: [PATCH] add remote --- .DS_Store | Bin 0 -> 6148 bytes build => discord repl/build | 0 discord repl/main.cpp | 199 ++++++++++++++++++++++++++ timercpp.h => discord repl/timercpp.h | 0 main/build | 1 + main.cpp => main/main.cpp | 0 main/timercpp.h | 50 +++++++ 7 files changed, 250 insertions(+) create mode 100644 .DS_Store rename build => discord repl/build (100%) create mode 100644 discord repl/main.cpp rename timercpp.h => discord repl/timercpp.h (100%) create mode 100755 main/build rename main.cpp => main/main.cpp (100%) create mode 100644 main/timercpp.h diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..271cb4fb86b47aeb1d7ef440ef246c85eda808bd GIT binary patch literal 6148 zcmeHKy-EW?5T1z#Mof`1VBt1`Ac}c|Gn}1;kTQ+NNEC8j@Tb2=@FlElwXwCd@F9Ew zUqJAi-67tRA1p*fW?<*ro1fje?~dCWBI3@?piR^yqB@3PZ3SwEh+oE()YQ@dvaB&0 zw!0@qKAws+!(U}Uo?SvaG^BI7j-Oxepz6zuEbEVoK5X^3<&DGbwdcLqKUnA=pH<$C z+AdP5K?Cf1MJH6yCFTzLl*TlQ>fncuMX}Yow$?WvZ(F{uxohXeSDoL9kLu=ksXL*Z zj#2qVRHt|4B*Tbsjmh!Z44L##=wLD z`93%d!Bnvf=sz7$z6Ag#FnhsVdI`yKim7555FQAJRA5MDZ812c!|pULRV)LBbYg8j zSa)XaP}uK|^_>qVP6L`Y28@B2fu(TSl=FXoe*YgQ*_AP14E!qwILW$M2e+hW>(tH3 uS?e&4FhnG-3^+-_#2>|om7} +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "timercpp.h" + +using namespace std; + +void beforeExit(); +void beforeExit(int i); +void setCursor(int XPos, int YPos); +void getCursor(int* x, int* y); +void hideCursor(); +void showCursor(); +void clearScreen(); +void clearScreen(short width, short height); +void writeAt(int x, int y, char ch); +void printFrame(vector> screen, short width, short height); +void screen_type(vector> &screen, string str, int x, int y, bool vertical); +void eachFrame(vector> &screen, short width, short height); + +Timer t = Timer(); + +int main(int argc, char **argv) +{ + + //^C + signal(SIGINT, beforeExit); + //abort() + signal(SIGABRT, beforeExit); + //sent by "kill" command + signal(SIGTERM, beforeExit); + //^Z + signal(SIGTSTP, beforeExit); + atexit(beforeExit); + + float framerate = 10.0; + float frameduration = 1000.0/framerate; + + vector> screen; // WILL ALWAYS BE AT LEAST 1x1 + + struct winsize w; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); + // width: w.ws_col + // height: w.ws_row + + for (int i = 0; i < w.ws_row-1; i++) + cout << endl; + + hideCursor(); + + t.setInterval([&]() { + ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); + + if (w.ws_row > 0 && w.ws_col > 0) { + + //fill vector + screen.resize(w.ws_col); + for (auto& col : screen) + col.resize(w.ws_row); + + eachFrame(screen, w.ws_col, w.ws_row); + + printFrame(screen, w.ws_col, w.ws_row); + } + }, (int) frameduration); + + while (true); + + return 0; +} + +void beforeExit() +{ + //clearScreen(); + setCursor(0,0); + showCursor(); + t.stop(); + cout << "goodbye" << endl; + exit(0); +} + +void beforeExit(int i) +{ + beforeExit(); +} + +void setCursor(int XPos, int YPos) { + printf("\033[%d;%dH",YPos+1,XPos+1); +} +void getCursor(int* x, int* y) { + printf("\033[6n"); + scanf("\033[%d;%dR", x, y); +} + +void hideCursor() +{ + cout << "\e[?25l" << flush; +} + +void showCursor() +{ + cout << "\e[?25h" << flush; +} + +void writeAt(int x, int y, char ch) +{ + if (ch == 0) + ch == ' '; + setCursor(x, y); + //printf("%s", ch); + cout << ch << flush; + fflush(stdout); +} + +void clearScreen() { + struct winsize w; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); + clearScreen(w.ws_col, w.ws_row); +} + +void clearScreen(short width, short height) +{ + setCursor(0, 0); + for (int i = 0; i < height; i++) + for (int j = 0; j < width; j++) + writeAt(j, i, ' '); + setCursor(0, 0); + +} + +void printFrame(vector> screen, short width, short height) +{ + //print frame + for (int y = 0; y < height; y++) + { + for (int x = 0; x < width; x++) + //cout << screen.at(x).at(y); + writeAt(x, y, screen.at(x).at(y)); + //cout << endl; + } + setCursor(0,0); + /*for (auto& col : screen) { + for (auto& ch : col) + cout << ch; + cout << endl; + }*/ + //cout << endl; +} + +void screen_type(vector> &screen, string str, int x, int y, bool vertical) +{ + + int width = 0, + height = 0; + + if (str.length()) { + + //str.append("\033[0m"); + + width = 1; + height = 1; + + if (vertical) + height = str.length(); + else + width = str.length(); + + width = min(width, (int) screen.size() - x); + height = min(height, (int) screen.at(0).size() - y); + + } + + for (int i = x, ii = 0, d = 0; ii < width; i++, ii++) + for (int j = y, jj = 0; jj < height; j++, jj++, d++) { + //cout << str.at(d) << " " << i << "," << j << endl; + screen.at(i).at(j) = str.at(d); + } +} + +void eachFrame(vector> &screen, short width, short height) +{ + + stringstream ss; + + ss << width << "x" << height; + //ss << "Hello World."; + + screen_type(screen, ss.str(), 10, 10, false); + +} \ No newline at end of file diff --git a/timercpp.h b/discord repl/timercpp.h similarity index 100% rename from timercpp.h rename to discord repl/timercpp.h diff --git a/main/build b/main/build new file mode 100755 index 0000000..319104d --- /dev/null +++ b/main/build @@ -0,0 +1 @@ +g++-10 -fconcepts-ts $1.cpp -o $1.o && chmod +x $1.o diff --git a/main.cpp b/main/main.cpp similarity index 100% rename from main.cpp rename to main/main.cpp diff --git a/main/timercpp.h b/main/timercpp.h new file mode 100644 index 0000000..9878c06 --- /dev/null +++ b/main/timercpp.h @@ -0,0 +1,50 @@ +// https: //github.com/99x/timercpp/blob/master/timercpp.h +#include +#include +#include + +class Timer +{ + bool clear = false; + + public: + void setTimeout(auto function, int delay); + void setInterval(auto function, int interval); + void stop(); +}; + +void Timer::setTimeout(auto function, int delay) +{ + this->clear = false; + std::thread t([=]() { + if (this->clear) + return; + std::this_thread::sleep_for(std::chrono::milliseconds(delay)); + if (this->clear) + return; + function(); + }); + t.detach(); +} + +void Timer::setInterval(auto function, int interval) +{ + this->clear = false; + std::thread t([=]() { + while (true) + { + if (this->clear) + return; + std::this_thread::sleep_for(std::chrono::milliseconds(interval)); + if (this->clear) + return; + function(); + } + }); + t.detach(); +} + +void Timer::stop() +{ + this->clear = true; +} \ No newline at end of file