Saturday, March 19, 2011

Digital clock source code


All most all game/graphics library provides timing function but If you create a game such as puzzle games or quiz games without graphics then you have to write your own procedure. In C programming language time.h library function is available to access clock time. Here I have provided a sample digital timer application written in C using time.h library function.


#include <stdio.h>
#include <time.h>         //this is header file for time
#include <windows.h>

COORD coord = {0, 0};
void gotoxy (int x, int y)
{
    coord.X = x;
    coord.Y = y; // X and Y coordinates
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void delay(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}

int main()
{

    long i = 0;                      /* Loop counter              */
    clock_t now = 0;                 /* Holds initial clock time  */
    int interval = 1;                /* Seconds interval for o/p  */
    int elapsed = 0;
    int min=0,MIN=0,hrs=0,sec=0;
    int d=0,f=0;
    now = clock(); /* Get current clock time    */
    
    for(i = 0L ;  ; i++)
    {
        elapsed = (clock()-now)/CLOCKS_PER_SEC;

        if(elapsed>=interval)
        {
            interval += 1;
            if(elapsed%60==0)
            {
                min=elapsed/60;
                d=60*min;
                if(min%60==0)
                {
                    hrs=min/60;
                    f=60*hrs;
                }
            }
            sec=elapsed-d;
            MIN=min-f;
            if(hrs<10)
            {
                gotoxy(2,4);
                printf("0%d",hrs);
            }
            else
            {
                gotoxy(2,4);
                printf(":%d",hrs);
            }
            if(min<10)
            {
                gotoxy(4,4);
                printf(":0%d",MIN);
            }
            else
            {
                gotoxy(4,4);
                printf(":%2d",MIN);
            }

            if(sec<10)
            {
                gotoxy(7,4);
                printf(":0%d",sec);
            }
            else
            {
                gotoxy(7,4);
                printf(":%2d",sec);
            }
            gotoxy(7,7);
            printf("%dhrs:%dmin:%dsec",hrs,MIN,sec);
        }
    }

    return 0;
}

Resize and Move Console Window in C/C++ using windows function


This is another article related to the window.h library function. Here I will take you through resizing the console window and moving it on the screen at the desire position. If you are not using any graphics library then you will not have control over the console window easily. Using window.h library function is the only option. Also, I will also like to inform you that we are using windows function so it will not work on Unix/Linux machine.
The sample problem is written in C language and compile in the GCC compiler.

#include <windows.h>
#include <stdio.h>

HWND WINAPI GetConsoleWindowNT(void)
{
    //declare function pointer type
    typedef HWND WINAPI(*GetConsoleWindowT)(void);
    //declare one such function pointer
    GetConsoleWindowT GetConsoleWindow;
    //get a handle on kernel32.dll
    HMODULE hk32Lib = GetModuleHandle(TEXT("KERNEL32.DLL"));
    //assign procedure address to function pointer
    GetConsoleWindow = (GetConsoleWindowT)GetProcAddress(hk32Lib,TEXT("GetConsoleWindow"));
    //check if the function pointer is valid
    //since the function is undocumented
    if(GetConsoleWindow == NULL){
        return NULL;
    }
    //call the undocumented function
    return GetConsoleWindow();
}
int main()
{
    HWND hWnd=GetConsoleWindowNT();
    MoveWindow(hWnd,1230,600,300,200,TRUE);
}

Change console windows background color in code::block


In my earlier article changing text color in codeblock and background color of console, I had provided a sample source code to change text color and text background color using windows.h library function. Similarly, we can also change console windows background color using windows.h library function. Here, I have shared a sample source code to change the console windows background color. The sample program is written in C.

#include <windows.h>          //header file for windows
#include <stdio.h>

void ClearConsoleToColors(int ForgC, int BackC);
int main()
{
    ClearConsoleToColors(0,1);
    Sleep(1000);
    return 0;
}
void ClearConsoleToColors(int ForgC, int BackC)
{
    WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
    //Get the handle to the current output buffer...
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    //This is used to reset the carat/cursor to the top left.
    COORD coord = {0, 0};
    //A return value... indicating how many chars were written
    //   not used but we need to capture this since it will be
    //   written anyway (passing NULL causes an access violation).
    DWORD count;

    //This is a structure containing all of the console info
    // it is used here to find the size of the console.
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    //Here we will set the current color
    SetConsoleTextAttribute(hStdOut, wColor);
    if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
    {
        //This fills the buffer with a given character (in this case 32=space).
        FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);

        FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count );
        //This will set our cursor position for the next print statement.
        SetConsoleCursorPosition(hStdOut, coord);
    }
    return;
}
You can change console windows background color by passing integer value range 0 up-to 256 in ClearConsoleToColors(int ForgC, int BackC) function.

Text background color of console windows using C in code::block


In my earlier article changing text color in codeblocks, I had provided sample source code to change text color using windows.h library function. Similarly, we can also change text background color using windows.h library function. Here, I have shared sample source code to change text background color. The sample program is written in C.

#include <windows.h>
#include <stdio.h>
void SetColorAndBackground(int ForgC, int BackC);
int main()
{
    SetColorAndBackground(5,1);   //color value range 0 up-to 256
    printf("what is text background color");
    Sleep(1000);
    return 0;
}
void SetColorAndBackground(int ForgC, int BackC)
{
     WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
     return;
}
You can change text background color by passing integer value range 0 up-to 256 in SetColorAndBackground(int ForgC, int BackC) function. First parameter is for text color and second parameter is for background color.

changing text color in codeblock

Changing text color in console applications created using the GCC compiler in codeblocks is challenging until an unless you use the graphic library. Unlike turbo c/c++ GCC does not provide the function to change text color. If you want to change the text color to make console windows colorful then we can use windows.h library function. Here in this article, I will share sample code to change text color. The sample program is written in C using windows.h library function.

#include <windows.h>         //header file for windows
#include <stdio.h>           //C standard library
void SetColor(int ForgC);
int main()
{
    SetColor(34);
    printf("test color");
    return 0;
}
void SetColor(int ForgC)
{
     WORD wColor;
                          //We will need this handle to get the current background attribute
     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
     CONSOLE_SCREEN_BUFFER_INFO csbi;

                           //We use csbi for the wAttributes word.
     if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
     {
                     //Mask out all but the background attribute, and add in the forgournd color
          wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
          SetConsoleTextAttribute(hStdOut, wColor);
     }
     return;
}
you can change your desire text color by passing integer value range 0 up-to 256 in SetColor( ) function.

Thursday, March 17, 2011

gotoxy in code::block

gotoxy(int x, int y) a function available in Turbo C/C++. It is not standard C. This function is used to move the cursor on the screen to the desire location. The top left corner of the monitor is 0,0 and the bottom right corner might be any number based on the size of the screen. But today's standard C++ compiler such as Visual Studio, GCC and clang to not provide gotoxy function. However, if you are developing console applications without a third party graphic library then gotoxy function is useful to manage text alignment on the screen.

If you need similar implementation on a windows machine with GCC compiler then here is an example source code.
#include <windows.h>    //  header file for gotoxy
#include <iostream> //header file for standard input output
COORD coord= {0,0}; // this is global variable
void gotoxy(int x,int y)
{
    coord.X=x;
    coord.Y=y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}

int main()
{
    //calling these function
    int x;
    gotoxy(15,5);
    std::cout<<"1. This is item number one.";
    gotoxy(15,7);
    std::cout<<"2. This is item number two.";
    gotoxy(15,9);
    std::cout<<"3. This is item number three.";
    gotoxy(15,11);
    std::cout<<"4. This is item number four.";
    gotoxy(15,13);
    std::cout<<"5. This is item number five.";
    std::cin>>x;
    return 0;
}
Please watch the video below for details explanation.

Sunday, March 13, 2011

Mini project "library management" in C programming


      This is my college 1st-semester mini-project “library management “ in C programming language. This is a console application without graphics and compiled in code::block with GCC compiler. I hope it will help you to develop your own project similar project related to file handling for example “Banking “,” customer records”,” student records" etc.
I have divided my project into many functions and I will describe each of them which may help you to understand better.