Posts

Showing posts from January, 2015

C++ Console : Basic Graphic Techniques

Image
     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"<<ch...

C++ Console : Keyboard Input for Games

Usually we need arrow keys input for simple games like luddo, tic-tac-toe or any board game. Here is a simplified code for getting input from arrow keys of keyboard. As you read code you can easily edit it for your own choice buttons and return values of the input function. #include <iostream> #include <windows.h> #include <conio.h> using namespace std; int getArrowKeyInput(); void main(){ char user = getArrowKeyInput(); while (user != 'e'){ cout << user << endl; user = getArrowKeyInput(); } system("pause"); } int getArrowKeyInput(){ int input = _getch(); char return_value; if (GetAsyncKeyState(VK_DOWN)){ _getch(); return_value = 'd'; } else if (GetAsyncKeyState(VK_RIGHT)){ _getch(); return_value = 'r'; } else if (GetAs...

Diamond and other Shapes in Console : Make them hollow

Image
     To get the idea of printing a shape in c++ console, read this article . Here we'll discuss how will we implement hollowness in the shapes that we made in previous article.      First revise the code of square, easy wasn't it? So is to implement hollowness in it. We just need some if statements to bypass the process of filling the shape. OK, have a look in nested loop of square code: #include <iostream> using namespace std; void main(){ int width = 8, height = 5; char custom_character = '*'; for (int i = 0; i < height; i++) { for (int i = 0; i < width; i++) { cout << custom_character; } cout << endl; } system("pause"); }      What we need to do here is that when this loop starts, it should print 1 character and then for rest n-2 character it should print some character which will have no shape like SPACE. It'll show hollowness in shape and similarl...

C++ Console : Change Font Size

Image
Font size is not a very big deal but i had to post it's code here because we'll be using it to change the fonts of our console in such ratio that will help us making geometric shapes like squares, easily and perfectly. Here is a list of available ratios that a console supports. You can use either of them: 4:6 6:8 8:8 16:8 5:12 7:12 16:12 12:16 10:18 Code: #include <iostream> #include <Windows.h> using namespace std; HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); void fontsize(int,int); void main(){ fontsize(8, 8); cout << "This is a symmetrical font" << endl; system("pause"); } void fontsize(int a, int b){ PCONSOLE_FONT_INFOEX lpConsoleCurrentFontEx = new CONSOLE_FONT_INFOEX(); lpConsoleCurrentFontEx->cbSize = sizeof(CONSOLE_FONT_INFOEX); GetCurrentConsoleFontEx(out, 0, lpConsoleCurrentFontEx); lpConsoleCurrentFontEx->dwFontSize.X = a; lpConsoleCurrentFontEx-...

C++ Console : Full Screen

Image
     As soon as i grasped on programming, i felt like it was too boring to run programs in console where black and white and small screen is strictly applied on my head and i have to just go with it. But software is not a name of limit or stopping. It is meant to create anything which is not even possible in real world. So today i'm sharing with you a simple code which you can save and use in various C++ projects you can also visit this post where you can see how console is colored so that you can achieve some basic level of satisfaction. #include <iostream> #include <Windows.h> using namespace std; HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); void fullscreen(); void main(){ fullscreen(); cout << "Console is now in full screen size" << endl; system("pause"); } void fullscreen(){ COORD NewSBSize = GetLargestConsoleWindowSize(out); NewSBSize.Y += 3; NewSBSize.X -= 2; SMALL_RECT DisplayA...

Loops : Some Info

     Hi, conditional operators, variables are the base of any program apart from it's functionality to communicate with system. 2nd important thing is control that is the flow of statements. Now simple programs like hello world can do fine with those but when we start to make such programs which need to take big decisions and directions, we encounter a problem that is re-using a piece of code or in simple words repetition of a piece of code.      Loops are very helpful and can be considered basic of programming. Let's have a look at loop system how to use them and get some common error information. A loop is made up of 2 basic parts, one is it's body which contains the code that is going to be looped and 2nd is the condition around which we want to loop the code. both of course are very important. Without either one of them, loop is useless. Simplest loop is while loop. it describes basic functionality of loops: #include <iostream> using names...

Password Input in Console : How to Make It Look Like Password

Image
Hi, here is a small code to implement password like look in C++ console input, there is nothing much to say, just that you can use it as a function and return the value as string which will be the password. You can set the character printed instead of real input to make it look like whatever you prefer. Please let me know if there is any error or logical mistake in code. #include <iostream> #include <string> #include <conio.h> using namespace std; void main(){ string password; char temp = 0; while (true){ temp = _getch(); if (temp == 13) break; if ((int)temp == 8){ password = password.substr(0, password.length()-1); } else{ password = password + temp; } cout << '\r'; for (int i = 0; i <= password.length(); i++) cout << ' '; cout << '\r'; for (int i = 0; i < password.length(); i++){ cout << '*'; } } system(...

Diamond Shape in Console : The logic behind it

Image
     Further after understanding core concepts of loops in programming, we have some very interesting practice programs which can boost your programming understanding and concepts more then any thing in my view. Because programming includes art + math hence their practice makes one a good programmer. Let's have a look at some of these. Square         - Hollow Square Triangle       - Hollow Triangle Diamond      - Hollow Diamond         These shapes are based on 2D matrix of characters hence to print out these we use nested loops. If you are not familiar with loops or nested loops, read this article.      Here is how loops take care of these problems, n ested loops have 2 directions (assume). Main loop runs once and nested one runs more times. In the context of shape let's say a square of 5 x 5, we have to print 5 lines and in each line we have to print 5 characters....