diff --git a/App2/CMakeLists.txt b/App2/CMakeLists.txt index c24104a252..5d6a65483e 100644 --- a/App2/CMakeLists.txt +++ b/App2/CMakeLists.txt @@ -1,5 +1,9 @@ set(CMAKE_CXX_FLAGS "-std=c++11") -add_executable(Test test.cc Console.cc) +add_executable(Test + test.cc + Console.cc + Console.h + MacUtils.h) add_custom_command( OUTPUT Test.dsk COMMAND ${MAKE_APPL} -c Test diff --git a/App2/Console.cc b/App2/Console.cc index 44ef940049..1620bbca8e 100644 --- a/App2/Console.cc +++ b/App2/Console.cc @@ -1,16 +1,18 @@ #include "Console.h" +#include "MacUtils.h" +#include "Events.h" Console *Console::currentInstance = NULL; Console::Console(GrafPtr port) + : consolePort(port) { + PortSetter setport(consolePort); + Rect r = {2,2,340,510}; bounds = r; - SetPort(port); - consolePort = port; TextFont(9); TextSize(9); - MoveTo(10,10); cellSizeY = 10; cellSizeX = CharWidth('M'); @@ -39,6 +41,7 @@ void Console::DrawCell(short x, short y) void Console::Draw() { + PortSetter setport(consolePort); //PashortRect(&r); for(short row = 0; row < rows; ++row) @@ -63,6 +66,8 @@ void Console::ScrollUp(short n) void Console::putch(char c) { + PortSetter setport(consolePort); + //Debugger(); switch(c) { @@ -84,3 +89,44 @@ void Console::putch(char c) } } +std::string Console::ReadLine() +{ + std::string buffer; + EventRecord event; + char c; + + do + { + do + { + while(!GetOSEvent(everyEvent, &event)) + ; + } while(event.what != keyDown && event.what != autoKey); + + c = event.message & charCodeMask; + + if(c == '\r') + c = '\n'; + + if(c == '\b') + { + cursorX--; + putch(' '); + cursorX--; + + buffer.substr(0,buffer.size()-1); + + continue; + //c = 'X'; + } + if(c == 127) + { + c = 'Y'; + } + + putch(c); + buffer += std::string(1,c); + } while(c != '\n'); + return buffer; +} + diff --git a/App2/Console.h b/App2/Console.h index 5ceb5d0e79..8a10958423 100644 --- a/App2/Console.h +++ b/App2/Console.h @@ -1,5 +1,6 @@ #include #include +#include class Console { @@ -7,6 +8,7 @@ public: Console(GrafPtr port); void Draw(); void putch(char c); + std::string ReadLine(); static Console *currentInstance; private: diff --git a/App2/MacUtils.h b/App2/MacUtils.h new file mode 100644 index 0000000000..9a62af6fa0 --- /dev/null +++ b/App2/MacUtils.h @@ -0,0 +1,19 @@ +#include + +class PortSetter +{ + GrafPtr save; +public: + PortSetter(GrafPtr port) + { + ::GetPort(&save); + ::SetPort(port); + } + + ~PortSetter() + { + ::SetPort(save); + } +}; + + diff --git a/App2/test.cc b/App2/test.cc index f8f1168568..4ff7004af6 100644 --- a/App2/test.cc +++ b/App2/test.cc @@ -127,6 +127,9 @@ int main(int argc, char** argv) std::cout << "Hello, world: " << i << std::endl; std::cout << std::flush; + std::cout << "Say something: " << std::flush; + console.ReadLine(); + for(int i = 0; i < 5; i++) { std::cout << "Exception speed test: " << std::flush;