#include "console.h" #include #include "jdraw.h" #include "jinput.h" #include "room.h" #include "actor.h" #include "jgame.h" #include "debug.h" namespace console { bool enabled = false; bool initialized = false; char msg[80]; char cmd[80]; int cmd_size = 0; uint8_t cursor_blinker = 0; void execute_command(); void init() { strcpy(msg, "PoolOS v0.1 ready."); cmd[0] = '\0'; cmd_size = 0; initialized = true; } void toggle() { if (!initialized) return; enabled = !enabled; cmd[0] = '\0'; cmd_size = 0; } const bool isEnabled() { return enabled; } const char scancode_to_ascii(uint8_t scancode) { if (scancode == SDL_SCANCODE_SPACE) return ' '; if (scancode == SDL_SCANCODE_0 || scancode == SDL_SCANCODE_KP_0) return '0'; if (scancode >= SDL_SCANCODE_1 && scancode <= SDL_SCANCODE_9) return scancode+19; if (scancode >= SDL_SCANCODE_KP_1 && scancode <= SDL_SCANCODE_KP_9) return scancode-40; if (scancode >= SDL_SCANCODE_A && scancode <= SDL_SCANCODE_Z) return scancode+61; if (scancode == SDL_SCANCODE_MINUS) return '-'; if (scancode == SDL_SCANCODE_EQUALS) return '='; if (scancode == SDL_SCANCODE_LEFTBRACKET) return '['; if (scancode == SDL_SCANCODE_RIGHTBRACKET) return ']'; if (scancode == SDL_SCANCODE_BACKSLASH) return '\\'; if (scancode == SDL_SCANCODE_SEMICOLON) return ';'; if (scancode == SDL_SCANCODE_APOSTROPHE) return '<'; if (scancode == SDL_SCANCODE_COMMA) return ','; if (scancode == SDL_SCANCODE_PERIOD) return '.'; if (scancode == SDL_SCANCODE_SLASH) return '/'; return '\0'; } const bool update() { if (!initialized || !enabled) return false; const uint8_t scancode = input::getKeyPressed(); if (scancode != SDL_SCANCODE_UNKNOWN) { cursor_blinker = 0; if (scancode == SDL_SCANCODE_BACKSPACE) { if (cmd_size>0) cmd[--cmd_size] = '\0'; } else if (scancode == SDL_SCANCODE_UNKNOWN) { // do nothing } else if (scancode == SDL_SCANCODE_RETURN) { execute_command(); } else { char ascii = scancode_to_ascii(scancode); if (ascii != '\0') { cmd[cmd_size++] = ascii; cmd[cmd_size] = '\0'; } } } cursor_blinker=(cursor_blinker+1)&0x07; return true; } void draw() { if (!initialized || !enabled) return; draw::color(BLACK); draw::fillrect(1, 0, 318, 14); draw::color(GREEN); draw::rect(1, -1, 318, 15); draw::print(msg, 3, 1, WHITE, 0); draw::print(">", 3, 7, WHITE+LIGHT, 0); draw::print(cmd, 7, 7, WHITE+LIGHT, 0); if (cursor_blinker<4) { const int size = strlen(cmd); draw::print("_", 7+size*4, 7, WHITE+LIGHT, 0); } } char *tokens[4]; void tokenize() { int token = 0; for (int i=0; i<4; ++i) tokens[i]=nullptr; char *p = cmd; char *fin = cmd; while (true) { while (*fin!='\0' && *fin!=' ') fin++; tokens[token++] = p; if (*fin=='\0') return; *fin = '\0'; fin++; p = fin; } } #define CMD_UNKNOWN -1 #define CMD_GIVE 0 #define CMD_DROP 1 #define CMD_GOTO 2 #define CMD_MOVE 3 #define CMD_EXIT 4 #define CMD_SHOW 5 #define CMD_HIDE 6 #define NUM_CMDS 7 const char *command_text[NUM_CMDS] = { "GIVE", "DROP", "GOTO", "MOVE", "EXIT", "SHOW", "HIDE" }; #define PARAM_UNKNOWN -1 #define PARAM_RUN 0 #define PARAM_GOD 1 #define PARAM_JUMP 2 #define NUM_PARAMS 3 const char *param_text[NUM_PARAMS] = { "RUN", "GOD", "JUMP" }; const int getCommand() { for (int i=0; i list) { //static const char *skillset_name[4] = {"SHOES", "GLOVES", "PANTS", "BAG"}; for (int i=0;i