Download Console Window Graphics

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

Solaire

Respected Hacker
Dank Tier VIP
Dec 15, 2013
1,051
16,353
62
Yet another fun class for you all to use. Essentially, it allows you to draw on specific tiles in the console window easily without the constant *Clear screen* *Re print out everything*. What it does is hold a 2D array of tiles, each tile containing information about itself and a bool called toUpdate. Any time you change that tile, toUpdate becomes true, and when the render function is called the changes will be rendered to the console. It's much smoother and quicker than clearing the screen and redrawing every time.

The first class is called Console. It's essentially a wrapper around basic console functions to allow easier interaction with the console window.
The second class is called Wizrd. It contains the rendering functions and the 2D array of tiles.

Console.h
C++:
enum ConColor{
    BLACK,
    DARKBLUE,
    DARKGREEN,
    DARKCYAN,
    DARKRED,
    DARKPURPLE,
    DARKYELLOW,
    LIGHTGREY,
    GREY,
    BLUE,
    LIGHTGREEN,
    CYAN,
    RED,
    PURPLE,
    YELLOW,
    WHITE
};

class Console{
public:
    Console();
    ~Console();

    void out(char chr);
    void out(const char * text);
    void outCenter(const char * text);

    void vOut(const char * fmtText, ...);
    void vOutCenter(const char * fmtText, ...);

    void SetTextColor(ConColor col);
    void SetBackgroundColor(ConColor col);

    void SetCursorPosition(COORD position);
    void SetCursorVisible(bool vis);
    void SetTitle(const char * title);

    void Pause() { int charRead = getchar(); }

    COORD GetConsoleSize();
    COORD GetCursorPos();

private:
    HANDLE m_input;
    HANDLE m_output;
    HANDLE m_error;
};
Console.cpp
C++:
#include <Windows.h>
#include <stdio.h>
#include "Console.h"

Console::Console(){
    m_input = GetStdHandle(STD_INPUT_HANDLE);
    m_output = GetStdHandle(STD_OUTPUT_HANDLE);
    m_error = GetStdHandle(STD_ERROR_HANDLE);
}

Console::~Console(){
    CloseHandle(m_input);
    CloseHandle(m_output);
    CloseHandle(m_error);
}

void Console::out(char chr){
    printf("%c", chr);
}

void Console::out(const char * text){
    printf(text);
}

void Console::outCenter(const char * text){
    CONSOLE_SCREEN_BUFFER_INFOEX consoleInfo = { 0 };
    consoleInfo.cbSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX);
    GetConsoleScreenBufferInfoEx(m_output, &consoleInfo);

    COORD newPos;
    newPos.X = (consoleInfo.dwSize.X / 2) - (strlen(text) / 2);
    newPos.Y = consoleInfo.dwCursorPosition.Y;
    SetConsoleCursorPosition(m_output, newPos);

    printf(text);
}

void Console::vOut(const char * fmtText, ...){
    char temp[250];
    va_list vaList;
    va_start(vaList, fmtText);
    int const TextLen = vsprintf_s(temp, fmtText, vaList);
    va_end(vaList);

    printf(temp);
}

void Console::vOutCenter(const char * fmtText, ...){
    CONSOLE_SCREEN_BUFFER_INFOEX consoleInfo = { 0 };
    consoleInfo.cbSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX);
    GetConsoleScreenBufferInfoEx(m_output, &consoleInfo);

    char temp[250];
    va_list vaList;
    va_start(vaList, fmtText);
    int const TextLen = vsprintf_s(temp, fmtText, vaList);
    va_end(vaList);

    COORD newPos;
    newPos.X = (consoleInfo.dwSize.X / 2) - (TextLen / 2);
    newPos.Y = consoleInfo.dwCursorPosition.Y;
    SetConsoleCursorPosition(m_output, newPos);

    printf(temp);
}

void Console::SetTextColor(ConColor col){
    CONSOLE_SCREEN_BUFFER_INFOEX consoleInfo = { 0 };
    consoleInfo.cbSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX);
    GetConsoleScreenBufferInfoEx(m_output, &consoleInfo);

    DWORD backgroundMask = 0xF0;
    DWORD curBackground = consoleInfo.wAttributes & backgroundMask;
    DWORD newColor = curBackground | (DWORD)col;

    SetConsoleTextAttribute(m_output, (WORD)newColor);
}

void Console::SetBackgroundColor(ConColor col){
    CONSOLE_SCREEN_BUFFER_INFOEX consoleInfo = { 0 };
    consoleInfo.cbSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX);
    GetConsoleScreenBufferInfoEx(m_output, &consoleInfo);

    DWORD textColorMask = 0x0F;
    DWORD curText = consoleInfo.wAttributes & textColorMask;
    DWORD newColor = curText | ((DWORD)col << 4);

    SetConsoleTextAttribute(m_output, (WORD)newColor);
}

void Console::SetCursorPosition(COORD position){
    SetConsoleCursorPosition(m_output, position);
}

void Console::SetCursorVisible(bool vis){
    CONSOLE_CURSOR_INFO cursInf;
    GetConsoleCursorInfo(m_output, &cursInf);
    cursInf.bVisible = vis;
    SetConsoleCursorInfo(m_output, &cursInf);
}

void Console::SetTitle(const char * title){
    SetConsoleTitle(title);
}

COORD Console::GetConsoleSize(){
    CONSOLE_SCREEN_BUFFER_INFOEX consoleInfo = { 0 };
    consoleInfo.cbSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX);
    GetConsoleScreenBufferInfoEx(m_output, &consoleInfo);

    return consoleInfo.dwSize;
}

COORD Console::GetCursorPos(){
    CONSOLE_SCREEN_BUFFER_INFOEX consoleInfo = { 0 };
    consoleInfo.cbSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX);
    GetConsoleScreenBufferInfoEx(m_output, &consoleInfo);

    return consoleInfo.dwCursorPosition;
}
Wizrd.h
C++:
struct Tile{
    bool toUpdate;
    ConColor background;
    ConColor foreground;
    char chr;

    Tile() : toUpdate(false), background((ConColor)NULL), foreground((ConColor)NULL), chr(NULL) { }
};

class Wizrd{
public:
    Wizrd(int sizeX, int sizeY);
    ~Wizrd();

    void SetTileBackground(int x, int y, ConColor col);
    void SetTileForeground(int x, int y, ConColor col);
    void SetTileChar(int x, int y, char chr);

    void ChangeTile(int x, int y, ConColor backCol, ConColor frontCol, char chr = '\0');
    Tile * GetTile(int x, int y) { return &m_screen[y + x * m_sizeY]; }

    void Render();

    int m_sizeX;
    int m_sizeY;

private:
    Console m_con;
    Tile * m_screen;
};
Wizrd.cpp
C++:
#include <Windows.h>
#include <stdio.h>
#include "Console.h"
#include "Wizrd.h"

Wizrd::Wizrd(int sizeX, int sizeY){
    // Initialize the vars
    m_sizeX = sizeX;
    m_sizeY = sizeY;

    // Create the 2D screen tile array
    m_screen = new Tile[m_sizeX * m_sizeY];
}

Wizrd::~Wizrd(){
    delete[] m_screen;
}

void Wizrd::SetTileBackground(int x, int y, ConColor col){
    GetTile(x, y)->background = col;
    GetTile(x, y)->toUpdate = true;
}

void Wizrd::SetTileForeground(int x, int y, ConColor col){
    GetTile(x, y)->foreground = col;
    GetTile(x, y)->toUpdate = true;
}

void Wizrd::SetTileChar(int x, int y, char chr){
    GetTile(x, y)->chr = chr;
    GetTile(x, y)->toUpdate = true;
}

void Wizrd::ChangeTile(int x, int y, ConColor backCol, ConColor frontCol, char chr){
    SetTileBackground(x, y, backCol);
    SetTileForeground(x, y, frontCol);
    SetTileChar(x, y, chr);
}

void Wizrd::Render(){
    // Loop through the tiles
    for (int x = 0; x < m_sizeX; x++){
        for (int y = 0; y < m_sizeY; y++){
            // If the tile has been marked to update
            if (GetTile(x, y)->toUpdate){
                // Set the colors
                m_con.SetTextColor(GetTile(x, y)->foreground);
                m_con.SetBackgroundColor(GetTile(x, y)->background);
                // Set the cursor position and draw the tile
                m_con.SetCursorPosition({ x, y });
                m_con.out(GetTile(x, y)->chr);
                // The tile has been updated, so it no longer needs to be updated
                GetTile(x, y)->toUpdate = false;

                // Just to make sure we don't see the cursor anywhere in the screen
                m_con.SetCursorVisible(false);
            }
        }
    }
}
Here is some basic code on how to use the class. It's a little arena I suppose with a character you can move around. You'll find that the rendering is very quick and without random shuddering usually caused by clearing and redrawing.

C++:
#include <Windows.h>
#include <stdio.h>
#include "Console.h"
#include "Wizrd.h"

#define WIDTH 80
#define HEIGHT 25

int main(){
    Console con;
    Wizrd wiz(WIDTH, HEIGHT);

    // Set the title
    con.SetTitle("WizardHacks");

    // Fill in the background
    for (int i = 0; i < WIDTH; i++)
        for (int j = 0; j < HEIGHT; j++)
            wiz.ChangeTile(i, j, BLUE, BLACK);

    // Add the walls
    for (int i = 0; i < WIDTH; i++)
        wiz.ChangeTile(i, 0, DARKBLUE, CYAN, '#');

    for (int i = 0; i < WIDTH; i++)
        wiz.ChangeTile(i, HEIGHT - 1, DARKBLUE, CYAN, '#');

    for (int i = 0; i < HEIGHT; i++)
        wiz.ChangeTile(0, i, DARKBLUE, CYAN, '#');

    for (int i = 0; i < HEIGHT; i++)
        wiz.ChangeTile(WIDTH - 1, i, DARKBLUE, CYAN, '#');

    // Add player
    int pX = 1, pY = 1;
    wiz.ChangeTile(pX, pY, BLUE, LIGHTGREEN, '@');

    // Render
    while (1){
        wiz.Render();

        int dx = 0, dy = 0;
        if (GetAsyncKeyState(VK_UP) & 1) --dy;
        else if (GetAsyncKeyState(VK_DOWN) & 1) ++dy;
        else if (GetAsyncKeyState(VK_LEFT) & 1) --dx;
        else if (GetAsyncKeyState(VK_RIGHT) & 1) ++dx;

        // If the player has moved
        if (dx || dy){
            // If they are inside the walls
            if ((pX + dx >= 1) && (pX + dx <= WIDTH - 2) && (pY + dy >= 1) && (pY + dy <= HEIGHT - 2)){
                // Return their previous tile to it's original state
                wiz.ChangeTile(pX, pY, BLUE, BLACK);
                // Move them to their new position
                wiz.ChangeTile(pX += dx, pY += dy, BLUE, LIGHTGREEN, '@');
            }
        }
    }

    con.Pause();
}
As always, comments, tips, errors you find, etc are all very appreciated!

Have fun,
Krampus
 

Liduen

Hacker
Dank Tier VIP
May 19, 2013
702
8,478
33
Looks really well written! :)

If you'd be checking a very large console window for tile changes, it would be even faster to just store pointers to the tiles which changed instead of those 2 for loops in Wizrd::Render().
 

Solaire

Respected Hacker
Dank Tier VIP
Dec 15, 2013
1,051
16,353
62
Looks really well written! :)

If you'd be checking a very large console window for tile changes, it would be even faster to just store pointers to the tiles which changed instead of those 2 for loops in Wizrd::Render().
Not a bad idea, I really like that! The only trouble would be checking if the tile you've changed is already in the list, but that's probably an easy fix with a flag in the Tile struct :p. I'll see what I can do later :D
 
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods