Skip to content

Commit

Permalink
Linux - More compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
FLAK-ZOSO committed Jul 8, 2023
1 parent 5478414 commit 136f776
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,6 @@ g++ levelmaker.cpp -o levelmaker -std=c++17
...you will see a window like the one above, now you can create your own level by moving the cursor with `W`, `A`, `S`, `D` and placing or removing a block with `P` and `R`.

ℹ️ - The blocks are placed under the `$` character if possible
⚠️ - Due to problems with raw input, you will have to press `ENTER` after each key press
⚠️ - Due to problems with raw input, you will have to press `ENTER` after each key press, since `v0.5` this is no longer the case for Linux users.

Then you can save your level by pressing `Q` and the level will be saved in the `levels` folder.
Binary file added fullkning
Binary file not shown.
36 changes: 32 additions & 4 deletions fullkning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@

tcsetattr(0, TCSANOW, &noecho);
}
#elif __linux__
#include <unistd.h>
#include <termios.h>

char getch(void) {
char buf = 0;
struct termios old = {0};
fflush(stdout);
if(tcgetattr(0, &old) < 0)
perror("tcsetattr()");
old.c_lflag &= ~ICANON;
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
if(tcsetattr(0, TCSANOW, &old) < 0)
perror("tcsetattr ICANON");
if(read(0, &buf, 1) < 0)
perror("read()");
old.c_lflag |= ICANON;
old.c_lflag |= ECHO;
if(tcsetattr(0, TCSADRAIN, &old) < 0)
perror("tcsetattr ~ICANON");
// printf("%c\n", buf);
return buf;
}
#endif

#define WIDTH 10
Expand Down Expand Up @@ -326,8 +351,8 @@ int main(int argc, char* argv[]) {
return getch();
#elif __APPLE__
return getchar();
#else
return getchar();
#elif __linux__
return (int)getch();
#endif
});
while (future.wait_for(std::chrono::milliseconds(300)) != std::future_status::ready) {
Expand All @@ -346,6 +371,9 @@ int main(int argc, char* argv[]) {
std::cout << "Cooldown: " << std::max(game::frame_countdown, (short)0) << " ";
cursor.set(14, 15);
std::cout << "Selected: " << (game::hooked_block == BlockType::Sand ? "Sand" : "Stone") << " ";
#if __linux__ // Ubuntu 22.04 has a low refresh rate...
std::cout << std::flush;
#endif
}
if (victory())
break;
Expand Down Expand Up @@ -382,9 +410,9 @@ int main(int argc, char* argv[]) {
} else {
std::cout << "You won with " << game::score << " points!" << std::endl;
}
#ifdef _WIN32
#if defined(_WIN32) or defined(__linux__)
getch();
#else
#elif __APPLE__
getchar();
#endif
return 0;
Expand Down
Binary file added levelmaker
Binary file not shown.
27 changes: 26 additions & 1 deletion levelmaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@
#include <fstream>
#ifdef _WIN32
#include <conio.h>
#elif __linux__
#include <unistd.h>
#include <termios.h>

char getch(void) {
char buf = 0;
struct termios old = {0};
fflush(stdout);
if(tcgetattr(0, &old) < 0)
perror("tcsetattr()");
old.c_lflag &= ~ICANON;
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
if(tcsetattr(0, TCSANOW, &old) < 0)
perror("tcsetattr ICANON");
if(read(0, &buf, 1) < 0)
perror("read()");
old.c_lflag |= ICANON;
old.c_lflag |= ECHO;
if(tcsetattr(0, TCSADRAIN, &old) < 0)
perror("tcsetattr ~ICANON");
// printf("%c\n", buf);
return buf;
}
#endif

#define WIDTH 10
Expand Down Expand Up @@ -46,7 +71,7 @@ int main(int argc, char* argv[]) {
std::cout << "Use {Q}+ENTER to quit" << std::endl;

while (true) {
#ifdef _WIN32
#if _WIN32 or __linux__
char input = getch();
#elif __APPLE__
cursor_handler.set({20, 30});
Expand Down

0 comments on commit 136f776

Please sign in to comment.