C++ Console : Basic Graphic Techniques
Console is basically a text panel and this being the reason it is slow for rendering (printing) on it. There are again 2 ways to print on console. Pixel by pixel which is used for printing images and anything is possible with that but this method will yield slow printing speed and that is not very helpful in games or usual applications but in rare cases it may be helpful like making little bit of logos and graphs.
The 2nd method is to use predefined ASCII characters to make a whole shape. This is fast method but since characters are fairly bigger than single pixel (about 1:10 - +) ratio so they are not helpful in making curve shapes. Here is a link and code to get ASCII table characters view and select which one you might like to use:
#include <iostream>
using namespace std;
void main( ){
cout<<"value\tcharacter"<<endl;
for ( int i = 0; i <= 255; i++){
cout<<i<<"\t"<<char(i)<<endl;
}
}
By using just square, we can make lot's of graphic stuff. Here is an example of a game which is entirely made by square shapes and some static printing:
The easiest way to implement graphics like above is to make a function which will take parameters of size and location and print a square or some shape at that location of the given size. Here is the code for square which can print a simple square at given location:
#include <iostream>
#include <Windows.h>
using namespace std;
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
void simple_square(int,int,int,int);
void hollow_square(int,int,int,int);
void go(int, int);
void main(){
simple_square(1,1, 10,10);
hollow_square(20,1, 10,10);
system("pause");
}
void simple_square(int X, int Y, int width, int height){
for (int set1 = 0; set1<height; set1++)
{
go(X, Y); Y++;
for (int set2 = 0; set2<width; set2++){ cout << char(219); }cout << endl;
}
}
void hollow_square(int X, int Y, int width, int height)
{
for (int set1 = 0; set1<height; set1++, Y++)
{
go(X, Y);
if (set1 == 0 || set1 == height- 1) { for (int set2 = 0; set2<width; set2++) cout << char(219); continue; }
cout << char(219); go(X + (width - 1), Y); cout << char(219);
}
}
void go(int a, int b){
COORD cord;
cord.X = a;
cord.Y = b;
SetConsoleCursorPosition(out, cord);
}
That can be improved to make beautiful interface of a game. Here are some tips for it:
- Color it
- Make it perfect
Color:
Color code can be added to print these squares in some colors:Get color code and some information about here:
http://mycodecollection.blogspot.com/2014/12/console-fun-colors.html
Make it perfect:
As you can see that thoe i set the size of square to be 10 x 10 but it is not a perfect. The problem is that ratio of a single character is not 1:1 hence when we print in default font size they have un-symmetric output. Console have a font size of 8 x 8 which if setted can print these squares perfectly in size:
Get information about changing font size here:
http://mycodecollection.blogspot.com/2015/01/c-console-change-font-size.html
Very nice, thanks
ReplyDelete