Wednesday, December 9, 2015

REVIEW STRUCT, POINTER/REFERENCE, FUNCTION, and ARRAY

Hey, fellas! There're so many tutorial about C programming language that I've given to you. Now, I will review when the right time to use STRUCT? When the right time to use POINTER/REFERENCE? When the right time to use FUNCTION? When the right time to use ARRAY? And also I will give u example that uses combination of all the items above (STRUCT, POINTER/REFERENCE, FUNCTION, and ARRAY).
Okaaay here we goooo

First, STRUCT.
You can find my post about STRUCT to know more about STRUCT. I just want to review some explanation about STRUCT. STRUCT is the collection of variables of different types under a single name for better handling. When the right time to use STRUCT? When the programmers have a lot of data of different kinds that needs to be grouped together, it can be used to hold or to represent records from a database.
For the example, when you want to store records of students which consist of student name, address, phone number, age, student id, etc.
The syntax is:
struct struct_name {
         //Statments//
};



Second, POINTER or REFERENCE.
POINTER is a memory address. POINTER is not the variable, but it is the content of the variable. When the right time to use POINTER? When the programmers want to access the addresses of the variable and manipulate the contents of the variables. We must use POINTER correctly, it could improve the efficiency and performance of our program, but if we use it incorrectly, it could lead our program to many dangerous problem, from un-readable to infamous bugs such as memory leaks, etc.
The syntax is:
type *ptr;
    or
type* ptr;
    or
type * ptr; 

REFERENCE allows you to create a second name for the variable that you can use to read or modify the original data stored in the variable. When we declare a REFERENCE and assign it variable, it will allow us to treat the REFERENCE exactly as thought it were original variable for the purpose.
The syntax is:
type& reference = variable;


Third, FUNCTION.
FUNCTION is a module of code that takes information in, does some computation, and returns a new piece of information based on the parameter informations. A function declaration tells the compiler about a function's name, return type, and parameter. When program call the function, the program control is transferred to the called function. A called function performs a defined task and when it return statement is executed. There're 3 aspects in C FUNCTION, they are:

No.C function aspectssyntax
1function definition
(contains all the statements
to be executed)
return_type function_name ( arguments list )
{ Body of function; }
2function call
(calls the actual functions)
function_name ( arguments list );
3function declaration
(informs compiler about
the function name, parameters,
and return value's data type)
return_type function_name ( argument list );



Last, ARRAY.
ARRAY is sequence of data item of same type. ARRAY is used to store collection of data, but it is often more useful to thin of an ARRAY as a collection of variable of the same type. All ARRAY consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. ARRAY allows us to use a for loop to access the elements by their index. When writing embedded software or an operating system an ARRAY may often be the better choice. While an ARRAY offers less functionality, it takes up less RAM, and the compiler can optimized code more efficiently for looks-up into ARRAY. The look-up time for a random access is much faster for lots of memory locations that containing the values, only taking a constant number of operations.
The syntax is:
type_name[number of elements];

Here some examples of combination STRUCT, POINTER/REFRENCE, FUNCTION,and ARRAY

1.
#include <stdio.h>
#include <string.h>

struct student {
     int id;
     char name[9];
     float score;
};
int main() {
    struct student record1 = {2, "Nikita", 85.5};
    struct student *ptr;
    ptr = &record1;    
    printf("Records of STUDENT: \n");
    printf("Id: %d\n", ptr->id);
    printf("Name: %s\n", ptr->name);
    printf("Score: %f\n", ptr->score);
    return 0;
}


Output:
Records of STUDENT:

Id: 2
Name: Nikita
Score: 85.500000

2.  
#include <stdio.h>
typedef struct complex {
    float real;
    float imag;
} complex;
complex add(complex n1,complex n2);
int main() {
    complex n1,n2,temp;
    printf("For 1st complex number \n");
    printf("Enter real and imaginary respectively:\n");
    scanf("%f%f",&n1.real,&n1.imag);
    printf("\nFor 2nd complex number \n");
    printf("Enter real and imaginary respectively:\n");
    scanf("%f%f",&n2.real,&n2.imag);
    temp=add(n1,n2);
    printf("Sum=%.1f+%.1fi",temp.real,temp.imag);
    return 0;
}
complex add(complex n1,complex n2) {
      complex temp;
      temp.real=n1.real+n2.real;
      temp.imag=n1.imag+n2.imag;
      return(temp);
}


Output:
For 1st complex number
Enter real and imaginary respectively:

For 2nd complex number
Enter real and imaginary respectively: 


OK!
That's all from me! Hope u will understand! As usually, have a great time with coding! Jesus bless u! 






Monday, December 7, 2015

HOW TO LOAD PNG IMAGE AND DISPLAY IT TO SCREEN USING SDL

Hey fellas! Now, I want to explain you about SDL. SDL is Simple DirectMedia Layer. SDL is a cross-platform development library designed to provide low level access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL and Direct3D. SDL officially supports Windows, Mac OS X, Linux, iOS, and Android. Support for the other platforms may be found in the source code. SDL is written in C, works natively with C++, and there are bindings available for several other languages, including C# and Python.
And now, I will explain about HOW TO LOAD PNG IMAGE AND DISPLAY IT TO SCREEN USING SDL.

 Here, I'm using SDL2. In SDL2, we can load image using the SDL_LoadBMP() function. But, unfortunately, that function doesnt provide us to work with the other image formats. However, such functionality is provided by an extension library called SDL_image.

First, make a new project. And don't forget to add the location of library and includepath to the .pro file. Because we use library SDL_image, so don't forget to put -lSDL2_image after the location of the library.



After that, let's go to the source code~~
oiya, don't forget to place the image in your Debug folder, where your executable is located.
This is the source code, if u want to load the png image and display it to the screen.


#include<SDL2/SDL.h>
#include<SDL2/SDL_image.h>

int main(int argc, char** argv)
{
    bool quit = false;
    SDL_Event event;

    SDL_Init(SDL_INIT_VIDEO);
    IMG_Init(IMG_INIT_PNG);

    SDL_Window * window = SDL_CreateWindow("SDL2 Displaying Image PNG",
        SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);

    SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
    SDL_Surface * image = IMG_Load("IMG_4457.png");
    SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, image);

    while (!quit)
    {
        SDL_WaitEvent(&event);

        switch (event.type)
        {
            case SDL_QUIT:
                quit = true;
                break;
        }
        SDL_RenderCopy(renderer, texture, NULL, NULL);
        SDL_RenderPresent(renderer);
    }

    SDL_DestroyTexture(texture);
    SDL_FreeSurface(image);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);

    IMG_Quit();
    SDL_Quit();

    return 0;
}



and when we compile it, the picture will appear in the new window with name "SDL2 Displaying Image PNG".

Okay, that's all from me. Now, it's your turn to try this with your own laptop or pc wkwkwk
As usually, have a great time with coding!


Sunday, December 6, 2015

HOW TO DISPLAYING TEXT USING SDL2

Hey, coding-ers! wkwk
In the last post, I've already explain about SDL and How to load image and display it in SDL, right? Now, I will give you the another tutorial using SDL2. We will learn about HOW TO DISPLAYING TEXT USING SDL2. YEAY!
Here we go....
When we want to display an image we use SDL_image library. But now we want to display text. We will use SDL_ttf library now! To setting it up is almost same to how we setting up SDL_image.
First, make a new project. Dont forget to write the location of includepath and library in .pro file. And also add -lSDL2_ttf after the location of library.


This is the source code for displaying the text...
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h> //include the relevant header file//

int main(int argc, char ** argv)
{
    bool quit = false;
    SDL_Event event;

    SDL_Init(SDL_INIT_VIDEO);
    TTF_Init(); //initialise the SDL_ttf library//

    SDL_Window * window = SDL_CreateWindow("SDL_ttf in SDL2",
        SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640,
        480, 0);
    SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
    TTF_Font * font = TTF_OpenFont("calibri.ttf", 25);
    //load a font into memory// 
    SDL_Color color = { 255, 255, 255 };
    SDL_Surface * surface = TTF_RenderText_Solid(font,
     "Welcome to My SDL", color);//the text//
    SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, surface);

    while (!quit)
    {
        SDL_WaitEvent(&event);

        switch (event.type)
        {
            case SDL_QUIT:
                quit = true;
                break;
        }
    }

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_DestroyTexture(texture);
    SDL_FreeSurface(surface);
    SDL_RenderCopy(renderer, texture, NULL, NULL);
    SDL_RenderPresent(renderer);

    TTF_Quit();//shut down the SDL_ttf//
    SDL_Quit();

    return 0;
}

And when we compile+run this project, it will show a window with text that we've made inside the window. It is cool, isn't it?~
It's almost the same with displaying the image right??? I believe that you can try it by yourself at home wkwkwk
Okay, it's your turn guys! Have a great time with coding luv!