Skip to content
Merged

Dev #10

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
18 changes: 5 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,8 @@ add_executable(idleGame src/main.cpp
src/InputMonitor.cpp
include/InputMonitor.h
src/Settings.cpp
include/Settings.h)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_executable(debug src/debug.cpp
src/Game.cpp
include/Game.h
src/Buyables.cpp
include/Buyables.h
src/InputMonitor.cpp
include/InputMonitor.h
src/Settings.cpp
include/Settings.h
include/displayUtils.h)
endif()
include/Settings.h
src/i18n.cpp
include/i18n.h
src/base64.cpp
include/base64.h)
8 changes: 6 additions & 2 deletions include/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ class Game {
public:
std::string username;
std::string statMessage;
std::string fakeInputBuffer;
std::atomic<bool> bIsRunning;
std::atomic<bool> bInputMode;
std::atomic<bool> bIsDisplayOnHalt;
const int FRAMERATE = 40;
const int AUTO_INCREMENT_RATE = 40;
const int FRAMERATE = 60;
const int AUTO_INCREMENT_RATE = 60;
const int CLICK_COOLDOWN = 10;
int iTimeCounter = 0;
bool bSpammingFlag = false;
bool bPathInputFlag = false;
int iClickIncrement;
int iAutoIncrement;
int iOptionIdx;
Expand All @@ -58,6 +60,8 @@ class Game {
void displayMenu();
void displayUpgrade();
void displayShop();
bool updatePathInput(char ch);
std::string getuserName() const;
static void displayMainMenu();

void handleKey(int ch);
Expand Down
8 changes: 7 additions & 1 deletion include/InputMonitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <atomic>
#include <functional>
#include <thread>
#include <string>
#ifdef __linux__
#include <termcap.h>
#include <termios.h>
Expand Down Expand Up @@ -33,14 +34,19 @@ class InputMonitor {
void stop();
void pause();
void resume();
void prePathInput();
void postPathInput();
std::string readLine(const std::string& prompt, const std::string& defaultInput = "");

private:
std::atomic<bool> bRunning;
std::thread monitorThread;
void initInput();
void resetInput();
int pollInput();
#ifndef _WIN32
char pollPath();
#ifdef _WIN32
#else
struct termios oldt;
int oldf;
#endif
Expand Down
8 changes: 8 additions & 0 deletions include/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#define SETTINGS_H
#include "InputMonitor.h"
#include <vector>
#include <__filesystem/filesystem_error.h>

#include "Game.h"

class Game;
class Settings {
Expand All @@ -24,6 +27,10 @@ class Settings {

void displaySettingsExportSave(); // $username.ids

void exportSaveFile(std::filesystem::path path);

void importSaveFile(std::filesystem::path path);

void displaySettingsLoadSave();

void setUsername();
Expand All @@ -34,6 +41,7 @@ class Settings {
std::vector<int> theFunnyNumber;
int uLevel;
int bLevel;
std::string userName;
};

};
Expand Down
22 changes: 22 additions & 0 deletions include/base64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Created by Calcite on 26-2-15.
//

#ifndef BASE64_H
#define BASE64_H
#include <string>


class base64 {
public:

static char base64_chars[];

static std::string encode(const std::string& input);

static std::string decode(const std::string& input);
};



#endif //BASE64_H
16 changes: 16 additions & 0 deletions include/i18n.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Created by Calcite on 25/09/20.
//

#ifndef I18N_H
#define I18N_H



class i18n {

};



#endif //I18N_H
82 changes: 50 additions & 32 deletions src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Game::Game() {
buyConfirm = nullptr;
settingConfirm = nullptr;
monitor->start([this](int ch){this->handleKey(ch);});
username = "";
}
Game::~Game() {
theFunnyNumber.clear();
Expand All @@ -39,52 +40,50 @@ Game::~Game() {
bIsRunning.store(false);
}
void Game::initWindow() {
int LINE_WIDTH = 0;
constexpr int DEFAULT_HEIGHT = 25;
constexpr int DEFAULT_WIDTH = 80;

int lineWidth = 0;

#ifdef _WIN64
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (GetConsoleScreenBufferInfo(hConsole, &csbi)) {
LINE_HEIGHT = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
LINE_WIDTH = csbi.srWindow.Right - csbi.srWindow.Left + 1;
} else {
LINE_HEIGHT = 25;
LINE_WIDTH = 80;
}
#else
struct winsize win;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0) {
LINE_HEIGHT = win.ws_row;
LINE_WIDTH = win.ws_col;
} else {
LINE_HEIGHT = 25;
LINE_WIDTH = 80;
}
#endif

while (LINE_HEIGHT < 18 || LINE_WIDTH < 40) {
std::cout << MAGENTA << "this terminal window seems too small" << nl;
std::cout << MAGENTA << "current window size is "
<< LINE_WIDTH << "*" << LINE_HEIGHT << nl;
std::cout << MAGENTA << "try resize this window to continue?" << nl << nl;

std::this_thread::sleep_for(std::chrono::milliseconds(200));

#ifdef _WIN64
auto getSize = [&]() {
if (GetConsoleScreenBufferInfo(hConsole, &csbi)) {
LINE_HEIGHT = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
LINE_WIDTH = csbi.srWindow.Right - csbi.srWindow.Left + 1;
lineWidth = csbi.srWindow.Right - csbi.srWindow.Left + 1;
} else {
LINE_HEIGHT = DEFAULT_HEIGHT;
lineWidth = DEFAULT_WIDTH;
}
};
#else
struct winsize win;
auto getSize = [&] {
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0) {
LINE_HEIGHT = win.ws_row;
LINE_WIDTH = win.ws_col;
lineWidth = win.ws_col;
} else {
LINE_HEIGHT = DEFAULT_HEIGHT;
lineWidth = DEFAULT_WIDTH;
}
};
#endif

getSize();

while (LINE_HEIGHT < 18 || lineWidth < 40) {
std::cout << MAGENTA << "This terminal window seems too small\n"
<< "Current size: " << lineWidth << "x" << LINE_HEIGHT << "\n"
<< "Try resize this window to continue.\n\n";

std::this_thread::sleep_for(std::chrono::milliseconds(200));
getSize();
clear_screen();
}
}


void Game::gameRun() {
if (!bIsDisplayOnHalt.load()) {
clear_screen();
Expand Down Expand Up @@ -195,10 +194,12 @@ void Game::displayShop(){
std::cout << "Press 'y' to buy, 'm' to return to main menu" << nl;
}



void Game::handleKey(int ch) {
if (bInputMode.load()){return;}
if (bPathInputFlag) {
updatePathInput(ch);
return;
}
std::lock_guard lk(stateMutex);
switch (ch) {
case 'a':
Expand Down Expand Up @@ -313,6 +314,20 @@ void Game::click() {
}
}

bool Game::updatePathInput(char ch) {
if (ch == 13) {
bPathInputFlag = false;
return true;
}
if (ch == 8) {
ch = '/b';
if (!fakeInputBuffer.empty()) {
fakeInputBuffer.pop_back();
}
}
fakeInputBuffer.push_back(ch);
return false;
}

void Game::buyUpgrade(int idx) {
if (idx < 0 || idx >= upgrades.size()) return;
Expand All @@ -332,6 +347,9 @@ void Game::buyUpgrade(int idx) {
statMessage = "success!";
}

std::string Game::getuserName() const{
return username;
}

void Game::buyBuilding(int idx) {
if (idx < 0 || idx >= buildings.size()) return;
Expand Down
61 changes: 59 additions & 2 deletions src/InputMonitor.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "InputMonitor.h"

#include <iostream>
#include <thread>
#ifdef __linux__
#include <unistd.h>
Expand Down Expand Up @@ -34,7 +35,7 @@ void InputMonitor::start(const Callback& callback)
if (ch!=-1) {
callback(ch);
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
resetInput();
});
Expand All @@ -60,14 +61,38 @@ int InputMonitor::pollInput() {
}
void InputMonitor::pause() { paused.store(true); resetInput(); }
void InputMonitor::resume() { initInput(); paused.store(false); }
std::string InputMonitor::readLine(const std::string& prompt, const std::string& defaultInput) {
std::string buffer = defaultInput;
std::cout << prompt << buffer << std::flush;

while (true) {
int ch = _getch();

if (ch == '\r') { // Enter
std::cout << std::endl;
break;
} else if (ch == 8) { // Backspace (ASCII 8)
if (!buffer.empty()) {
buffer.pop_back();
std::cout << "\b \b" << std::flush;
}
} else if (ch >= 32 && ch < 127) { // Printable characters
buffer += static_cast<char>(ch);
std::cout << static_cast<char>(ch) << std::flush;
}
}
std::cout << std::endl; // Newline on exit
return buffer;
}
#else
void InputMonitor::initInput() {
struct termios newt;
struct termios newt = {};
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);

// set non-blocking
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
}
Expand All @@ -82,7 +107,39 @@ int InputMonitor::pollInput() {
if (ch != EOF) return ch;
return -1;
}

void InputMonitor::pause() { paused.store(true); resetInput(); }
void InputMonitor::resume() { initInput(); paused.store(false); }
std::string InputMonitor::readLine(const std::string& prompt, const std::string& defaultInput) {
struct termios oldt = {}, newt = {};
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);

std::string buffer = defaultInput;
std::cout << prompt << buffer << std::flush;

while (true) {
char ch;
if (read(STDIN_FILENO, &ch, 1) > 0) {
if (ch == '\n') { // Enter
std::cout << std::endl;
break;
} else if (ch == 127 || ch == '\b') { // Backspace
if (!buffer.empty()) {
buffer.pop_back();
// Basic ANSI code: Move cursor back, overwrite with space, move back again
std::cout << "\b \b" << std::flush;
}
} else if (static_cast<unsigned char>(ch) >= 32 && ch != 127) {
buffer += ch;
std::cout << ch << std::flush;
}
}
}

tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return buffer;
}
#endif
Loading
Loading