test app with console

This commit is contained in:
Wolfgang Thaller 2012-03-29 10:31:05 +02:00
parent cc2f5140a8
commit 8dbff71e48
3 changed files with 267 additions and 0 deletions

86
App2/Console.cc Normal file
View File

@ -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<char>(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');
}
}

29
App2/Console.h Normal file
View File

@ -0,0 +1,29 @@
#include <Quickdraw.h>
#include <vector>
class Console
{
public:
Console(GrafPtr port);
void Draw();
void putch(char c);
static Console *currentInstance;
private:
GrafPtr consolePort;
Rect bounds;
std::vector<char> 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);
};

152
App2/test.cc Normal file
View File

@ -0,0 +1,152 @@
#include <stdio.h>
#include <string.h>
#include <string>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <Quickdraw.h>
#include <MacMemory.h>
#include <Sound.h>
#include <Events.h>
#include <Fonts.h>
#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;
}