From 8dbff71e485d1ab3c50feb848323c2a16545bfb3 Mon Sep 17 00:00:00 2001 From: Wolfgang Thaller Date: Thu, 29 Mar 2012 10:31:05 +0200 Subject: [PATCH] test app with console --- App2/Console.cc | 86 +++++++++++++++++++++++++++ App2/Console.h | 29 +++++++++ App2/test.cc | 152 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 267 insertions(+) create mode 100644 App2/Console.cc create mode 100644 App2/Console.h create mode 100644 App2/test.cc diff --git a/App2/Console.cc b/App2/Console.cc new file mode 100644 index 0000000000..44ef940049 --- /dev/null +++ b/App2/Console.cc @@ -0,0 +1,86 @@ +#include "Console.h" + +Console *Console::currentInstance = NULL; + +Console::Console(GrafPtr port) +{ + Rect r = {2,2,340,510}; + bounds = r; + SetPort(port); + consolePort = port; + TextFont(9); + TextSize(9); + MoveTo(10,10); + + cellSizeY = 10; + cellSizeX = CharWidth('M'); + + rows = (bounds.bottom - bounds.top) / cellSizeY; + cols = (bounds.right - bounds.left) / cellSizeX; + chars = std::vector(rows*cols, ' '); + + cursorX = cursorY = 0; + + currentInstance = this; +} + +Rect Console::CellRect(short x, short y) +{ + return { (short) (bounds.top + y * cellSizeY), (short) (bounds.left + x * cellSizeX), + (short) (bounds.top + (y+1) * cellSizeY), (short) (bounds.left + (x+1) * cellSizeX) }; +} +void Console::DrawCell(short x, short y) +{ + Rect r = CellRect(x,y); + EraseRect(&r); + MoveTo(r.left, r.bottom - 2); + DrawChar(chars[y * cols + x]); +} + +void Console::Draw() +{ + //PashortRect(&r); + + for(short row = 0; row < rows; ++row) + { + for(short col = 0; col < cols; ++col) + { + DrawCell(col, row); + } + } +} + +void Console::ScrollUp(short n) +{ + cursorY--; + std::copy(chars.begin() + cols, chars.end(), chars.begin()); + std::fill(chars.end() - cols, chars.end(), ' '); + //Console::Draw(); + RgnHandle rgn = NewRgn(); + ScrollRect(&bounds, 0, -cellSizeY, rgn); + DisposeRgn(rgn); +} + +void Console::putch(char c) +{ + //Debugger(); + switch(c) + { + case '\r': + cursorX = 0; + break; + case '\n': + cursorY++; + cursorX = 0; + if(cursorY >= rows) + ScrollUp(); + break; + default: + chars[cursorY * cols + cursorX] = c; + DrawCell(cursorX, cursorY); + cursorX++; + if(cursorX >= cols) + putch('\n'); + } +} + diff --git a/App2/Console.h b/App2/Console.h new file mode 100644 index 0000000000..5ceb5d0e79 --- /dev/null +++ b/App2/Console.h @@ -0,0 +1,29 @@ +#include +#include + +class Console +{ +public: + Console(GrafPtr port); + void Draw(); + void putch(char c); + + static Console *currentInstance; +private: + GrafPtr consolePort; + Rect bounds; + + std::vector chars; + + short cellSizeX; + short cellSizeY; + + short rows, cols; + + short cursorX, cursorY; + + Rect CellRect(short x, short y); + void DrawCell(short x, short y); + void ScrollUp(short n = 1); +}; + diff --git a/App2/test.cc b/App2/test.cc new file mode 100644 index 0000000000..f8f1168568 --- /dev/null +++ b/App2/test.cc @@ -0,0 +1,152 @@ +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "Console.h" + +QDGlobals qd; +__attribute((constructor)) +void cons() +{ + SysBeep(20); +} + +//float something = 6.0; + +class Foo +{ +}; + +void foobar() +{ + throw Foo(); +} + +void foobaz() +{ +} + +extern ssize_t (*__write_hook)(int fd, const void*buf, size_t count); + +extern "C" ssize_t consolewrite(int fd, const void *buf, size_t count) +{ + const char *p = (const char*)buf; + for(int i = 0; i < count; i++) + Console::currentInstance->putch(*p++); + return count; +} + +int main(int argc, char** argv) +{ + Rect r; + unsigned char buf[256]; + //foo = bar; + r.top = r.left = 0; + r.bottom = 342; + r.right = 512; + //= { 0, 0, 342, 512 }; + //Debugger(); + GrafPort port; + InitGraf(&qd.thePort); + InitFonts(); + OpenPort(&port); + EraseRect(&r); +#if 0 + MoveTo(100,100); + /*buf[0] = 2; + buf[1] = 'H'; + buf[2] = 'W';*/ + +#if 0 + sprintf((char*)&buf[1], "Hello, world: %d", 6*7);//6.0 * 7.0); + buf[0] = strlen((char*)&buf[1]); +#else + std::ostringstream out; + out << "Hello, world: " << 6*7; + std::string str = out.str(); + buf[0] = str.size(); + std::copy(str.begin(), str.end(), &buf[1]); +#endif + DrawString(buf); + + /*try + { + foobar(); + } + catch(...) + { + PaintRect(&r); + }*/ + //SysBeep(20); +#endif + /*try + { + FillRect(&r,&qd.gray); + foobar(); + PaintRect(&r); + } + catch(...) + { + EraseRect(&r); + }*/ + // + //if(!std::malloc(32)) + // PaintRect(&r); + new char[32]; + Console console(&port); + __write_hook = &consolewrite; + // console.putch('a'); + // console.putch('b'); + console.putch('x'); + console.putch('a'); + console.putch('b'); + //new char[42]; + console.putch('c'); + console.putch('a'); + console.putch('a'); + Console::currentInstance->putch('Y'); + printf("Hello...\n"); + console.putch('b'); + console.putch('\n'); + + std::cout << "Hello, world.\n"; + //printf("Hello, world.\n"); + console.putch('a'); + Console::currentInstance->putch('Y'); + std::cout << "X\n"; + for(int i = 1; i <= 100; i++) + std::cout << "Hello, world: " << i << std::endl; + std::cout << std::flush; + + for(int i = 0; i < 5; i++) + { + std::cout << "Exception speed test: " << std::flush; + long start = TickCount(); + try { foobar(); } catch(...) {} + long end = TickCount(); + std::cout << (end-start)*1000 / 60.0 << " ms\n"; + } + std::cout << "Click mouse 10 times...\n"; + //console.Draw(); + for(int i = 0; i < 10; i++) + { + //InvertRect(&qd.screenBits.bounds); + while(!Button()) + ; + while(Button()) + ; + std::cout << "Click #" << i + 1 << std::endl; + //console.Draw(); + } + // something *= 7.0; + return 0; +}