2016-06-04 3 views
0

저는 학교 프로젝트를 통해 C를 사용하여 Zilog Z8 앙코르 보드에 게임을 코딩합니다. 이것은 ANSI를 사용하여 작성되며 보드에는 콘솔 출력이 있으며 직렬 인터페이스를 통해 putty를 사용하여 읽습니다. 그러나, 내 Windows 10 컴퓨터를 사용하여, 나는 주위에 하드웨어가 필요없이 코드를 테스트 할 수 있도록 프로그램을 시뮬레이션 할 수 싶습니다. 문제는 내가 하드웨어 컨트롤러 대신 stdio를 사용하는 VS 2015를 사용하여 시도한 다음 콘솔에 보냅니다. 그러나 windows 명령 프롬프트는 ANSI 이스케이프 시퀀스를 표시하지 않습니다. 따라서 ANSI를 처리 할 수있는 conemu 설치를 시도했지만 전경색 만 변경하지는 않습니다.C와 Visual Studio를 사용하는 ANSI 2015

#include <string.h> 
#include <stdio.h> 

#define ESC 0x1B 

void fgcolor(int foreground) { 
    /* Value  foreground  Value  foreground 
    ------------------------------------------------ 
    0  Black   8  Dark Gray 
    1  Red    9  Light Red 
    2  Green   10  Light Green 
    3  Brown   11  Yellow 
    4  Blue   12  Light Blue 
    5  Purple   13  Light Purple 
    6  Cyan   14  Light Cyan 
    7  Light Gray  15  White 
    */ 
    int type = 22;    // normal text 
    if (foreground > 7) { 
     type = 1;    // bold text 
     foreground -= 8; 
    } 
    printf("%c[%d;%dm", ESC, type, foreground + 30); 
} 

void bgcolor(int background) { 
    /* IMPORTANT: When you first use this function you cannot get back to true white background in HyperTerminal. 
    Why is that? Because ANSI does not support true white background (ANSI white is gray to most human eyes). 
    The designers of HyperTerminal, however, preferred black text on white background, which is why 
    the colors are initially like that, but when the background color is first changed there is no 
    way comming back. 
    Hint:  Use resetbgcolor(); clrscr(); to force HyperTerminal into gray text on black background. 

    Value  Color 
    ------------------ 
    0  Black 
    1  Red 
    2  Green 
    3  Brown 
    4  Blue 
    5  Purple 
    6  Cyan 
    7  Gray 
    */ 
    printf("%c[%dm", ESC, background + 40); 
} 

void color(int foreground, int background) { 
    // combination of fgcolor() and bgcolor() - uses less bandwidth 
    int type = 22;    // normal text 
    if (foreground > 7) { 
     type = 1;    // bold text 
     foreground -= 8; 
    } 
    printf("%c[%d;%d;%dm", ESC, type, foreground + 30, background + 40); 
} 

void resetbgcolor() { 
    // gray on black text, no underline, no blink, no reverse 
    printf("%c[m", ESC); 
} 

void clrscr() { 
    printf("%c[2J", ESC); 
} 

void clreol() { 
    printf("%c[K", ESC); 
} 
void gotoxy(int x, int y) { 
    printf("%c[%d;%dH", ESC, y, x); 
} 

void underline(char on) { 
    if (on == 'y') { 
     printf("%c[4m", ESC); 
    } 
    else if (on == 'n') { 
     printf("%c[24m", ESC); 
    } 
} 

void blink(char on) { 
    if (on == 'y') { 
     printf("%c[5m", ESC); 
    } 
    else if (on == 'n') { 
     printf("%c[25m", ESC); 
    } 
} 

void reverse(char on) { 
    if (on == 'y') { 
     printf("%c[7m", ESC); 
    } 
    else if (on == 'n') { 
     printf("%c[27m", ESC); 
    } 
} 
void normal() { 
    printf("%c[0;22m", ESC); 
} 


void window(int x1, int y1, int x2, int y2, char * c, int stil) { 
    int length = strlen(c); 
    int i = 0; 
    char kanter[2][9] = { { 218,180,195,191,179,192,196,217 },{ 201,185,204,187,186,200,205,188 } }; 

    if (stil != 1) { 
     stil = 0; 
    } 


    //color(2,5); 
    gotoxy(x1, y1); 
    printf("%c%c", kanter[stil][0], kanter[stil][1]); 
    reverse('y'); 
    printf(" %s", c); 
    gotoxy(x1 + 4 + length, y1); 
    for (i = 0; i < x2 - (x1 + 5 + length); i++) { 
     printf(" "); 
    } 
    reverse('n'); 
    printf("%c%c", kanter[stil][2], kanter[stil][3]); 

    for (i = y1 + 1; i < y2; i++) { 
     gotoxy(x1, i); 
     printf("%c", kanter[stil][4]); 
     gotoxy(x2, i); 
     printf("%c", kanter[stil][4]); 
    } 

    gotoxy(x1, y2); 
    printf("%c", kanter[stil][5]); 
    for (i = 0; i < x2 - x1 - 1; i++) { 
     printf("%c", kanter[stil][6]); 
    } 
    printf("%c\n", kanter[stil][7]); 

    normal(); 
} 
void up(int x) { 
    printf("%c[%dA", ESC, x); 
} 

void down(int x) { 
    printf("%c[%dB", ESC, x); 
} 

void right(int x) { 
    printf("%c[%dC", ESC, x); 
} 
void left(int x) { 
    printf("%c[%dD", ESC, x); 
} 
void main() { 
    printf("hej"); 
    color(2, 0); 
    clrscr(); 
    printf("\n"); 
    window(3, 4, 20, 15, "hej", 1); 

    up(5); 
    right(5); 

    // window(21, 12, 35, 30, "Farvel", 0); 


    while (1 != 2) {}; 

} 

이 코드는 콘솔 내부에 윈도우를 생성, 다른 배경과 전경 색상 : 여기 시뮬레이션을 시도하고있는 코드입니다.

도움을 주시면 감사하겠습니다.

답변

1

Windows 10은 ANSI 시퀀스를 지원합니다! Visual Studio 내에서가 아니라 별도의 명령 프롬프트에서 .exe를 시작하십시오! Visual Studio에서 열리는 콘솔 창은 ANSI를 지원하지 않지만 표준 cmd.exe (표준 명령 프롬프트)은 지원합니다.

유용한 트릭은 .exe로 이동하는 것입니다. 파일 탐색기 창의 주소 표시 줄에 cmd을 입력 한 다음 enter 키를 누르십시오. 파일 탐색기에서 열었던 것과 같은 디렉토리로 설정된 현재 디렉토리가있는 콘솔이 열리 며 이는 매우 편리합니다.

+0

그건 다소 효과가있었습니다. 그것은 ConEmu를 사용하는만큼 길지만, 배경색은 변하지 않습니다. – Nothin

+0

흥미 롭습니다. 잠깐, 다른 OS에서 코드를 테스트 해 보겠습니다. – adam10603

+0

@ Nothin 배경을 검은 색으로 설정하는'color (2, 0)'를 호출하면 아무 것도 보이지 않습니다. 배경색으로 0 이외의 값을 사용하십시오. – adam10603