//--------------------------------------------------------------------------------- PrintConsole* consoleInit(PrintConsole* console, int layer, BgType type, BgSize size, int mapBase, int tileBase, bool mainDisplay, bool loadGraphics){ //--------------------------------------------------------------------------------- static bool firstConsoleInit = true; if(firstConsoleInit) { devoptab_list[STD_OUT] = &dotab_stdout; devoptab_list[STD_ERR] = &dotab_stdout; setvbuf(stdout, NULL , _IONBF, 0); setvbuf(stderr, NULL , _IONBF, 0); firstConsoleInit = false; } if(console) { currentConsole = console; } else { console = currentConsole; } *currentConsole = defaultConsole; if(mainDisplay) { console->bgId = bgInit(layer, type, size, mapBase, tileBase); } else { console->bgId = bgInitSub(layer, type, size, mapBase, tileBase); } console->fontBgGfx = (u16*)bgGetGfxPtr(console->bgId); console->fontBgMap = (u16*)bgGetMapPtr(console->bgId); console->consoleInitialised = 1; consoleCls('2'); if(loadGraphics) consoleLoadFont(console); return currentConsole; }
//--------------------------------------------------------------------------------- void consoleInit( int charBase, int mapBase, int background, const u8* font, int fontsize, int palette) { //--------------------------------------------------------------------------------- BGCTRL[background] = BG_SIZE_0 | CHAR_BASE(charBase) | SCREEN_BASE(mapBase); if (font == NULL || fontsize == 0) { font = amiga_fnt; fontsize = amiga_fnt_size; } upcvt_4bit(CHAR_BASE_ADR(charBase), font, fontsize); consoleMap = mapBase; consolePalette = palette; devoptab_list[STD_OUT] = &dotab_stdout; devoptab_list[STD_ERR] = &dotab_stderr; setvbuf(stderr, NULL , _IONBF, 0); setvbuf(stdout, NULL , _IONBF, 0); consoleCls(); consoleInitialised = true; }
//--------------------------------------------------------------------------------- ssize_t con_write(struct _reent *r,int fd,const char *ptr, size_t len) { //--------------------------------------------------------------------------------- char chr; int i, count = 0; char *tmp = (char*)ptr; if(!tmp || len<=0) return -1; i = 0; while(i<len) { chr = *(tmp++); i++; count++; if ( chr == 0x1b && *tmp == '[' ) { bool escaping = true; char *escapeseq = tmp++; int escapelen = 1; i++; count++; do { chr = *(tmp++); i++; count++; escapelen++; int parameter, assigned, consumed; // make sure parameters are positive values and delimited by semicolon if((chr >= '0' && chr <= '9') || chr == ';') continue; switch (chr) { //--------------------------------------- // Cursor directional movement //--------------------------------------- case 'A': consumed = 0; assigned = sscanf(escapeseq,"[%dA%n", ¶meter, &consumed); if (assigned==0) parameter = 1; if (consumed) currentConsole->cursorY = (currentConsole->cursorY - parameter) < 0 ? 0 : currentConsole->cursorY - parameter; escaping = false; break; case 'B': consumed = 0; assigned = sscanf(escapeseq,"[%dB%n", ¶meter, &consumed); if (assigned==0) parameter = 1; if (consumed) currentConsole->cursorY = (currentConsole->cursorY + parameter) > currentConsole->windowHeight - 1 ? currentConsole->windowHeight - 1 : currentConsole->cursorY + parameter; escaping = false; break; case 'C': consumed = 0; assigned = sscanf(escapeseq,"[%dC%n", ¶meter, &consumed); if (assigned==0) parameter = 1; if (consumed) currentConsole->cursorX = (currentConsole->cursorX + parameter) > currentConsole->windowWidth - 1 ? currentConsole->windowWidth - 1 : currentConsole->cursorX + parameter; escaping = false; break; case 'D': consumed = 0; assigned = sscanf(escapeseq,"[%dD%n", ¶meter, &consumed); if (assigned==0) parameter = 1; if (consumed) currentConsole->cursorX = (currentConsole->cursorX - parameter) < 0 ? 0 : currentConsole->cursorX - parameter; escaping = false; break; //--------------------------------------- // Cursor position movement //--------------------------------------- case 'H': case 'f': { int x, y; char c; if(sscanf(escapeseq,"[%d;%d%c", &y, &x, &c) == 3 && (c == 'f' || c == 'H')) { currentConsole->cursorX = x; currentConsole->cursorY = y; escaping = false; break; } x = y = 1; if(sscanf(escapeseq,"[%d;%c", &y, &c) == 2 && (c == 'f' || c == 'H')) { currentConsole->cursorX = x; currentConsole->cursorY = y; escaping = false; break; } x = y = 1; if(sscanf(escapeseq,"[;%d%c", &x, &c) == 2 && (c == 'f' || c == 'H')) { currentConsole->cursorX = x; currentConsole->cursorY = y; escaping = false; break; } x = y = 1; if(sscanf(escapeseq,"[;%c", &c) == 1 && (c == 'f' || c == 'H')) { currentConsole->cursorX = x; currentConsole->cursorY = y; escaping = false; break; } // invalid format escaping = false; break; } //--------------------------------------- // Screen clear //--------------------------------------- case 'J': if(escapelen <= 3) consoleCls(escapeseq[escapelen-2]); escaping = false; break; //--------------------------------------- // Line clear //--------------------------------------- case 'K': if(escapelen <= 3) consoleClearLine(escapeseq[escapelen-2]); escaping = false; break; //--------------------------------------- // Save cursor position //--------------------------------------- case 's': if(escapelen == 2) { currentConsole->prevCursorX = currentConsole->cursorX ; currentConsole->prevCursorY = currentConsole->cursorY ; } escaping = false; break; //--------------------------------------- // Load cursor position //--------------------------------------- case 'u': if(escapelen == 2) { currentConsole->cursorX = currentConsole->prevCursorX ; currentConsole->cursorY = currentConsole->prevCursorY ; } escaping = false; break; //--------------------------------------- // Color scan codes //--------------------------------------- case 'm': escapeseq++; escapelen--; do { parameter = 0; if (escapelen == 1) { consumed = 1; } else if (memchr(escapeseq,';',escapelen)) { sscanf(escapeseq,"%d;%n", ¶meter, &consumed); } else { sscanf(escapeseq,"%dm%n", ¶meter, &consumed); } escapeseq += consumed; escapelen -= consumed; switch(parameter) { case 0: // reset currentConsole->flags = 0; currentConsole->bg = 0; currentConsole->fg = 7; break; case 1: // bold currentConsole->flags &= ~CONSOLE_COLOR_FAINT; currentConsole->flags |= CONSOLE_COLOR_BOLD; break; case 2: // faint currentConsole->flags &= ~CONSOLE_COLOR_BOLD; currentConsole->flags |= CONSOLE_COLOR_FAINT; break; case 3: // italic currentConsole->flags |= CONSOLE_ITALIC; break; case 4: // underline currentConsole->flags |= CONSOLE_UNDERLINE; break; case 5: // blink slow currentConsole->flags &= ~CONSOLE_BLINK_FAST; currentConsole->flags |= CONSOLE_BLINK_SLOW; break; case 6: // blink fast currentConsole->flags &= ~CONSOLE_BLINK_SLOW; currentConsole->flags |= CONSOLE_BLINK_FAST; break; case 7: // reverse video currentConsole->flags |= CONSOLE_COLOR_REVERSE; break; case 8: // conceal currentConsole->flags |= CONSOLE_CONCEAL; break; case 9: // crossed-out currentConsole->flags |= CONSOLE_CROSSED_OUT; break; case 21: // bold off currentConsole->flags &= ~CONSOLE_COLOR_BOLD; break; case 22: // normal color currentConsole->flags &= ~CONSOLE_COLOR_BOLD; currentConsole->flags &= ~CONSOLE_COLOR_FAINT; break; case 23: // italic off currentConsole->flags &= ~CONSOLE_ITALIC; break; case 24: // underline off currentConsole->flags &= ~CONSOLE_UNDERLINE; break; case 25: // blink off currentConsole->flags &= ~CONSOLE_BLINK_SLOW; currentConsole->flags &= ~CONSOLE_BLINK_FAST; break; case 27: // reverse off currentConsole->flags &= ~CONSOLE_COLOR_REVERSE; break; case 29: // crossed-out off currentConsole->flags &= ~CONSOLE_CROSSED_OUT; break; case 30 ... 37: // writing color currentConsole->fg = parameter - 30; break; case 39: // reset foreground color currentConsole->fg = 7; break; case 40 ... 47: // screen color currentConsole->bg = parameter - 40; break; case 49: // reset background color currentConsole->fg = 0; break; } } while (escapelen > 0); escaping = false; break; default: // some sort of unsupported escape; just gloss over it escaping = false; break; } } while (escaping); continue; } consolePrintChar(chr); }
//--------------------------------------------------------------------------------- void consoleLoadFont(PrintConsole* console) { //--------------------------------------------------------------------------------- int i; u16* palette = BG_PALETTE_SUB; //check which display is being utilized if(console->fontBgGfx < BG_GFX_SUB){ palette = BG_PALETTE; } if(console->font.bpp == 4) { if(!console->font.convertSingleColor) { if(console->font.gfx) dmaCopy(console->font.gfx, console->fontBgGfx, console->font.numChars * 64 / 2); if(console->font.pal) dmaCopy(console->font.pal, palette + console->fontCurPal * 16, console->font.numColors*2); console->fontCurPal <<= 12; } else { console->fontCurPal = 15 << 12; for (i = 0; i < console->font.numChars * 16; i++) { u16 temp = 0; if(console->font.gfx[i] & 0xF) temp |= 0xF; if(console->font.gfx[i] & 0xF0) temp |= 0xF0; if(console->font.gfx[i] & 0xF00) temp |= 0xF00; if(console->font.gfx[i] & 0xF000) temp |= 0xF000; console->fontBgGfx[i] = temp; } //set up the palette for color printing palette[1 * 16 - 1] = RGB15(0,0,0); //30 normal black palette[2 * 16 - 1] = RGB15(15,0,0); //31 normal red palette[3 * 16 - 1] = RGB15(0,15,0); //32 normal green palette[4 * 16 - 1] = RGB15(15,15,0); //33 normal yellow palette[5 * 16 - 1] = RGB15(0,0,15); //34 normal blue palette[6 * 16 - 1] = RGB15(15,0,15); //35 normal magenta palette[7 * 16 - 1] = RGB15(0,15,15); //36 normal cyan palette[8 * 16 - 1] = RGB15(24,24,24); //37 normal white palette[9 * 16 - 1 ] = RGB15(15,15,15); //40 bright black palette[10 * 16 - 1] = RGB15(31,0,0); //41 bright red palette[11 * 16 - 1] = RGB15(0,31,0); //42 bright green palette[12 * 16 - 1] = RGB15(31,31,0); //43 bright yellow palette[13 * 16 - 1] = RGB15(0,0,31); //44 bright blue palette[14 * 16 - 1] = RGB15(31,0,31); //45 bright magenta palette[15 * 16 - 1] = RGB15(0,31,31); //46 bright cyan palette[16 * 16 - 1] = RGB15(31,31,31); //47 & 39 bright white } } else if(console->font.bpp == 8) { console->fontCurPal = 0; if(!console->font.convertSingleColor) { if(console->font.gfx) dmaCopy(console->font.gfx, console->fontBgGfx, console->font.numChars * 64); if(console->font.pal) dmaCopy(console->font.pal, palette, console->font.numColors*2); } else { for(i = 0; i < console->font.numChars * 16; i++) { u32 temp = 0; if(console->font.gfx[i] & 0xF) temp = 255; if(console->font.gfx[i] & 0xF0) temp |= 255 << 8; if(console->font.gfx[i] & 0xF00) temp |= 255 << 16; if(console->font.gfx[i] & 0xF000) temp |= 255 << 24; ((u32*)console->fontBgGfx)[i] = temp; } palette[255] = RGB15(31,31,31); } } palette[0] = RGB15(0,0,0); consoleCls('2'); }
//--------------------------------------------------------------------------------- ssize_t con_write(struct _reent *r,int fd,const char *ptr, size_t len) { //--------------------------------------------------------------------------------- char chr; int i, count = 0; char *tmp = (char*)ptr; int intensity = 0; if(!tmp || len<=0) return -1; i = 0; while(*tmp!='\0' && i<len) { chr = *(tmp++); i++; count++; if ( chr == 0x1b && *tmp == '[' ) { bool escaping = true; char *escapeseq = tmp; int escapelen = 0; do { chr = *(tmp++); i++; count++; escapelen++; int parameter; switch (chr) { ///////////////////////////////////////// // Cursor directional movement ///////////////////////////////////////// case 'A': siscanf(escapeseq,"[%dA", ¶meter); currentConsole->cursorY = (currentConsole->cursorY - parameter) < 0 ? 0 : currentConsole->cursorY - parameter; escaping = false; break; case 'B': siscanf(escapeseq,"[%dB", ¶meter); currentConsole->cursorY = (currentConsole->cursorY + parameter) > currentConsole->windowHeight - 1 ? currentConsole->windowHeight - 1 : currentConsole->cursorY + parameter; escaping = false; break; case 'C': siscanf(escapeseq,"[%dC", ¶meter); currentConsole->cursorX = (currentConsole->cursorX + parameter) > currentConsole->windowWidth - 1 ? currentConsole->windowWidth - 1 : currentConsole->cursorX + parameter; escaping = false; break; case 'D': siscanf(escapeseq,"[%dD", ¶meter); currentConsole->cursorX = (currentConsole->cursorX - parameter) < 0 ? 0 : currentConsole->cursorX - parameter; escaping = false; break; ///////////////////////////////////////// // Cursor position movement ///////////////////////////////////////// case 'H': case 'f': siscanf(escapeseq,"[%d;%df", ¤tConsole->cursorY , ¤tConsole->cursorX ); escaping = false; break; ///////////////////////////////////////// // Screen clear ///////////////////////////////////////// case 'J': consoleCls(escapeseq[escapelen-2]); escaping = false; break; ///////////////////////////////////////// // Line clear ///////////////////////////////////////// case 'K': consoleClearLine(escapeseq[escapelen-2]); escaping = false; break; ///////////////////////////////////////// // Save cursor position ///////////////////////////////////////// case 's': currentConsole->prevCursorX = currentConsole->cursorX ; currentConsole->prevCursorY = currentConsole->cursorY ; escaping = false; break; ///////////////////////////////////////// // Load cursor position ///////////////////////////////////////// case 'u': currentConsole->cursorX = currentConsole->prevCursorX ; currentConsole->cursorY = currentConsole->prevCursorY ; escaping = false; break; ///////////////////////////////////////// // Color scan codes ///////////////////////////////////////// case 'm': siscanf(escapeseq,"[%d;%dm", ¶meter, &intensity); //only handle 30-37,39 and intensity for the color changes parameter -= 30; //39 is the reset code if(parameter == 9){ parameter = 15; } else if(parameter > 8){ parameter -= 2; } else if(intensity){ parameter += 8; } if(parameter < 16 && parameter >= 0){ currentConsole->fontCurPal = parameter << 12; } escaping = false; break; } } while (escaping); continue; } consolePrintChar(chr); } return count; }
/*--------------------------------------------------------------------------------- consoleInit param: font: 16 color font charBase: the location the font data will be loaded to numCharacters: count of characters in the font charStart: The ascii number of the first character in the font set if you have a full set this will be zero map: pointer to the map you will be printing to. pal: specifies the 16 color palette to use, if > 15 it will change all non-zero entries in the font to use palette index 255 ---------------------------------------------------------------------------------*/ void consoleInit( u16* font, u16* charBase, u16 numCharacters, u8 charStart, u16* map, u8 pal, u8 bitDepth) { //--------------------------------------------------------------------------------- int i; row = col = 0; fontStart = charStart; fontOffset = 0; fontMap = map; if(bitDepth == 16) { if(pal < 16) { fontPal = pal << 12; for (i = 0; i < numCharacters * 16; i++) charBase[i] = font[i]; } else { fontPal = 15 << 12; for (i = 0; i < numCharacters * 16; i++) { u16 temp = 0; if(font[i] & 0xF) temp |= 0xF; if(font[i] & 0xF0) temp |= 0xF0; if(font[i] & 0xF00) temp |= 0xF00; if(font[i] & 0xF000) temp |= 0xF000; charBase[i] = temp; } } }//end if bitdepth else { fontPal = 0; for(i = 0; i < numCharacters * 16; i++) { u32 temp = 0; if(font[i] & 0xF) temp = 255; if(font[i] & 0xF0) temp |= 255 << 8; if(font[i] & 0xF00) temp |= 255 << 16; if(font[i] & 0xF000) temp |= 255 << 24; ((u32*)charBase)[i] = temp; } } devoptab_list[STD_OUT] = &dotab_stdout; devoptab_list[STD_ERR] = &dotab_stdout; setvbuf(stderr, NULL , _IONBF, 0); setvbuf(stdout, NULL , _IONBF, 0); consoleCls('2'); consoleInitialised = 1; }
//--------------------------------------------------------------------------------- int con_write(struct _reent *r,int fd,const char *ptr,int len) { //--------------------------------------------------------------------------------- if (!consoleInitialised) return -1; char chr; int i, count = 0; char *tmp = (char*)ptr; if(!tmp || len<=0) return -1; i = 0; while(*tmp!='\0' && i<len) { chr = *(tmp++); i++; count++; if ( chr == 0x1b && *tmp == '[' ) { bool escaping = true; char *escapeseq = tmp; int escapelen = 0; do { chr = *(tmp++); i++; count++; escapelen++; int parameter; switch (chr) { ///////////////////////////////////////// // Cursor directional movement ///////////////////////////////////////// case 'A': siscanf(escapeseq,"[%dA", ¶meter); row = (row - parameter) < 0 ? 0 : row - parameter; escaping = false; break; case 'B': siscanf(escapeseq,"[%dB", ¶meter); row = (row + parameter) > CONSOLE_HEIGHT - 1 ? CONSOLE_HEIGHT - 1 : row + parameter; escaping = false; break; case 'C': siscanf(escapeseq,"[%dC", ¶meter); col = (col + parameter) > CONSOLE_WIDTH - 1 ? CONSOLE_WIDTH - 1 : col + parameter; escaping = false; break; case 'D': siscanf(escapeseq,"[%dC", ¶meter); col = (col - parameter) < 0 ? 0 : col - parameter; escaping = false; break; ///////////////////////////////////////// // Cursor position movement ///////////////////////////////////////// case 'H': case 'f': siscanf(escapeseq,"[%d;%df", &row, &col); escaping = false; break; ///////////////////////////////////////// // Screen clear ///////////////////////////////////////// case 'J': consoleCls(escapeseq[escapelen-2]); escaping = false; break; ///////////////////////////////////////// // Line clear ///////////////////////////////////////// case 'K': consoleClearLine(escapeseq[escapelen-2]); escaping = false; break; ///////////////////////////////////////// // Save cursor position ///////////////////////////////////////// case 's': savedX = col; savedY = row; escaping = false; break; ///////////////////////////////////////// // Load cursor position ///////////////////////////////////////// case 'u': col = savedX; row = savedY; escaping = false; break; } } while (escaping); continue; } consolePrintChar(chr); } return count; }
//--------------------------------------------------------------------------------- ssize_t con_write(struct _reent *r,void *fileStruct ,const char *ptr,size_t len) { //--------------------------------------------------------------------------------- char chr; if (!consoleInitialised) return -1; int i, count = 0; char *tmp = (char*)ptr; if(!tmp || len<=0) return -1; i = 0; while(*tmp!='\0' && i<len) { chr = *(tmp++); i++; count++; if ( chr == 0x1b && *tmp == '[' ) { bool escaping = true; char *escapeseq = tmp; int escapelen = 0; do { chr = *(tmp++); i++; count++; escapelen++; int parameter; switch (chr) { case 'H': siscanf(escapeseq,"[%d;%dH", &consoleY, &consoleX); escaping = false; break; case 'f': siscanf(escapeseq,"[%d;%df", &consoleY, &consoleX); escaping = false; break; case 'A': siscanf(escapeseq,"[%dA", ¶meter); consoleY = (consoleY - parameter) < 0 ? 0 : consoleY - parameter; escaping = false; break; case 'B': siscanf(escapeseq,"[%dB", ¶meter); consoleY = (consoleY + parameter) > CONSOLE_HEIGHT - 1 ? CONSOLE_HEIGHT - 1 : consoleY + parameter; escaping = false; break; case 'C': siscanf(escapeseq,"[%dC", ¶meter); consoleX = (consoleX + parameter) > CONSOLE_WIDTH - 1 ? CONSOLE_WIDTH - 1 : consoleX + parameter; escaping = false; break; case 'D': siscanf(escapeseq,"[%dC", ¶meter); consoleX = (consoleX - parameter) < 0 ? 0 : consoleX - parameter; escaping = false; break; case 'K': escaping = false; break; case 's': savedX = consoleX; savedY = consoleY; escaping = false; break; case 'u': consoleX = savedX; consoleY = savedY; escaping = false; break; case 'J': if ( escapeseq[escapelen-2] == '2') { consoleCls(); consoleX = 0; consoleY = 0; } escaping = false; break; } } while (escaping); continue; } consolePrintChar(chr); } return count; }