add ConsoleTest application (test console without going through stdio/iostreams); make Console lib less likely to crash on bad_alloc

This commit is contained in:
Wolfgang Thaller 2015-09-08 23:47:46 +02:00
parent 4851123ac6
commit 8f9720a31e
5 changed files with 34 additions and 1 deletions

View File

@ -27,3 +27,8 @@ add_library(RetroConsole
install(TARGETS RetroConsole DESTINATION lib) install(TARGETS RetroConsole DESTINATION lib)
add_application(ConsoleTest
ConsoleTest.cc
)
target_link_libraries(ConsoleTest RetroConsole)

View File

@ -21,6 +21,7 @@
#include "MacUtils.h" #include "MacUtils.h"
#include "Events.h" #include "Events.h"
#include "Fonts.h" #include "Fonts.h"
#include "Processes.h"
#include <algorithm> #include <algorithm>
@ -31,6 +32,8 @@ Console *Console::currentInstance = NULL;
Console::Console(GrafPtr port, Rect r) Console::Console(GrafPtr port, Rect r)
: consolePort(port), bounds(r), dirtyRect() : consolePort(port), bounds(r), dirtyRect()
{ {
if(currentInstance == NULL)
currentInstance = (Console*) -1;
PortSetter setport(consolePort); PortSetter setport(consolePort);
InsetRect(&bounds, 2,2); InsetRect(&bounds, 2,2);
@ -42,7 +45,9 @@ Console::Console(GrafPtr port, Rect r)
rows = (bounds.bottom - bounds.top) / cellSizeY; rows = (bounds.bottom - bounds.top) / cellSizeY;
cols = (bounds.right - bounds.left) / cellSizeX; cols = (bounds.right - bounds.left) / cellSizeX;
chars = std::vector<char>(rows*cols, ' '); chars = std::vector<char>(rows*cols, ' ');
onscreen = chars; onscreen = chars;
cursorX = cursorY = 0; cursorX = cursorY = 0;

View File

@ -36,6 +36,9 @@ namespace Retro
std::string ReadLine(); std::string ReadLine();
static Console *currentInstance; static Console *currentInstance;
short GetRows() const { return rows; }
short GetCols() const { return cols; }
private: private:
GrafPtr consolePort; GrafPtr consolePort;
Rect bounds; Rect bounds;

16
Console/ConsoleTest.cc Normal file
View File

@ -0,0 +1,16 @@
#include "Console.h"
#include <string.h>
namespace Retro
{
void InitConsole();
}
int main()
{
Retro::InitConsole();
const char *s = "Hello, world.\n";
Retro::Console::currentInstance->write(s, strlen(s));
Retro::Console::currentInstance->ReadLine();
return 0;
}

View File

@ -63,6 +63,8 @@ extern "C" ssize_t _consolewrite(int fd, const void *buf, size_t count)
{ {
if(!Console::currentInstance) if(!Console::currentInstance)
InitConsole(); InitConsole();
if(Console::currentInstance == (Console*)-1)
return 0;
Console::currentInstance->write((const char*)buf, count); Console::currentInstance->write((const char*)buf, count);
return count; return count;
@ -72,6 +74,8 @@ extern "C" ssize_t _consoleread(int fd, void *buf, size_t count)
{ {
if(!Console::currentInstance) if(!Console::currentInstance)
InitConsole(); InitConsole();
if(Console::currentInstance == (Console*)-1)
return 0;
static std::string consoleBuf; static std::string consoleBuf;
if(consoleBuf.size() == 0) if(consoleBuf.size() == 0)