WriteConsoleOutputCharacter绘制多余像素的空白空间

时间:2018-09-09 00:32:35

标签: c++ windows console roguelike

我一直在控制台中设计一个流氓视频游戏。

我的问题是,我最初会立即使用writeconsoleoutput绘制整个地图,然后使用writeconsoleoutputcharacter重新绘制必要的内容。但我发现它在右侧绘制了一个额外的像素。 这实际上会删除右侧图块上的一个像素。

有人知道一种使它停止绘制字符并在字符右侧添加1像素空白的方法。

编辑:我发现一种解决方法是重新绘制玩家的台词,但一旦我能通知飞轮手,它就不会完美无缺。最好是仅逐个重绘字符,而结尾不附加任何像素。或其他任何地方。 用于打印字符的代码

void printchar(char character, COORD location,short color)
{
    const char *pointer = &character;
    //short LENGTH = 1;
    DWORD dwWritten = 0;
    std::vector<WORD> attributes;attributes.clear();;
    setAttributesChar(color,attributes);//Pushes back the color
    if(attributes.size() == 1)
    {    WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE),&attributes[0], attributes.size(), location, &dwWritten);
        WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), pointer, attributes.size(), location, &dwWritten); 
    }
}

void setAttributesChar(short color,std::vector<WORD> &attributes)
{
    switch(color)
    {
    case DEFAULTCOLOR:
        attributes.push_back(FOREGROUND_INTENSE_WHITE);return;
    case HOVEREDCOLOR:
        attributes.push_back(FOREGROUND_GREEN);return;
    case RED:
        attributes.push_back(FOREGROUND_RED);return;
    case GOLD:
        attributes.push_back(FOREGROUND_INTENSE_YELLOW);return;
    case REDWHITE:
        attributes.push_back(COMBINED_REDWHITE);return;
    case YELLOWPURPLE:
        attributes.push_back(COMBINED_YELLOWPURPLE);return;
    case NPCCOLOR:
        attributes.push_back(FOREGROUND_INTENSE_CYAN);return;
    case PURPLE:
        attributes.push_back(FOREGROUND_MAGENTA);return;
    case REDPURPLE:
        attributes.push_back(COMBINED_REDPURPLE);return;
    case BLUE:
        attributes.push_back(FOREGROUND_BLUE);return;
    case BLUEPURPLE:
        attributes.push_back(COMBINED_BLUEPURPLE);return;
    case INTENSEGREEN:
        attributes.push_back(FOREGROUND_INTENSE_GREEN);return;
    case REDGREEN:
        attributes.push_back(COMBINED_REDGREEN);return;
    case BWHITE:
        attributes.push_back(BACKGROUND_WHITE);return;
    }
}

示例:

字符过马路之前:BEFORE IMAGE

人物走过公路后:AFTER IMAGE

可执行链接:EXECUTABLE

我创建了一个可行的示例,您可以编译这些示例(假设您使用Windows)以查看发生了什么。在其中,我创建了一个控制台集,其大小用asci符号223填充屏幕。然后,我使用Writeconsoleoutchar打印绿色223符号。您会看到额外绘制的黑色像素线。

#include <windows.h>
#include <cwchar>
#include <vector>
enum COLORS{HOVEREDCOLOR};
void setAttributesChar(short color,std::vector<WORD> &attributes)
{
    switch(color)
    {
    case HOVEREDCOLOR:
        attributes.push_back(FOREGROUND_GREEN);return;
    }
}
void printchar(char character, COORD location,short color)
{
    const char *pointer = &character;
    //short LENGTH = 1;
    DWORD dwWritten = 0;
    std::vector<WORD> attributes;attributes.clear();;
    setAttributesChar(color,attributes);//Pushes back the color
    if(attributes.size() ==1)
    {      WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE),&attributes[0], attributes.size(), location, &dwWritten);
        WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), pointer, attributes.size(), location, &dwWritten); 
    }
}
#define SCREENWIDTH 1550
#define SCREENHEIGHT 850
void SETWINDOWUP(DWORD &SIZE)
{
    CONSOLE_SCREEN_BUFFER_INFO info;
    MoveWindow(GetConsoleWindow(),0,0,SCREENWIDTH,SCREENHEIGHT,true);         //MOVE WINDOW TO 0,0 AND MAKE 1600 BY 880
   ::SetWindowPos(GetConsoleWindow(), HWND_TOPMOST, 0, 0, 0,0,     SWP_DRAWFRAME | SWP_NOREPOSITION | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW); //SET WNDOWS POSITION COMPARED TO OTHER WINDOWS IN DRAW ORDER
::SetWindowPos(GetConsoleWindow(),HWND_NOTOPMOST, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
HANDLE hout = GetStdHandle( STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hout,&info);
COORD newBuffersize =
{
    info.srWindow.Right - info.srWindow.Left + 1,
    info.srWindow.Bottom - info.srWindow.Top + 1
};
SetConsoleScreenBufferSize(hout,newBuffersize);
SIZE = info.dwSize.X * info.dwSize.Y;
//EDIT REGULATED FONT SIZE
CONSOLE_FONT_INFOEX cfi;
std::wcscpy(cfi.FaceName, L"Lucida Console"); // Choose your font
cfi.FontFamily = FF_DONTCARE;
cfi.nFont = 0;
cfi.dwFontSize.X = 0;
cfi.dwFontSize.Y = 25;                  // Height
cfi.FontWeight = FW_BOLD;
cfi.cbSize = sizeof(cfi);
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), false, &cfi);
}
int main()
{
Sleep(500);DWORD SIZE = 0;SETWINDOWUP(SIZE);
DWORD charswritten;COORD start = {0,0};TCHAR CHARACTER = 223;
    FillConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE),CHARACTER,SIZE,start,&charswritten);
Sleep(500);
printchar(223,{6,5},HOVEREDCOLOR);
Sleep(500);
return 0;
};

0 个答案:

没有答案