Learn More

Tuesday, July 12, 2011

Basic Programming In SDL

sdl
After you successfully install SDL in CodeBlocks, it is time you learn the basic SDL. First, open the SDL project again in CodeBlocks. After that you will see the contents of the file main.cpp (this is the default name of each program you are in CodeBlocks) in the form


You will see the script # ifdef __cplusplus until the script # endif. This script is the default setting of the SDL in the CodeBlocks. The main function of this script is to ensure the type of software you use. And at the location where you put the SDL file.

Inside int main script, you'll see some script which is the parent of the SDL program. You can create several functions before the int main, if you want. But our advice you should make some function before the int main  of your program to run faster. In the next tutorial you will hopefully see the intent of some of these functions.

Each simulation always requires the initial state. Therefore SDL requires the initial state, the command to create the initial state is very simple, SDL_Init (SDL_INIT_VIDEO), this command function is to provide the initial state SDL video.

The next command is atexit (SDL_Quit) which serves to terminate the functions of SDL. When in a state program atexit (SDL_Quit) are met then the application will end your game.

After that you must make an application window. Include what size the window and the image quality you'll use in the game. Its function is SDL_SetVideoMode (640, 480, 16, SDL_HWSURFACE | SDL_DOUBLEBUF).

Once you are ready to make the application window, it is time for you to load the whole image file is required in making the game. SDL_LoadBMP ("cb.bmp") command function to load an image with bmp extension, besides the extension, then you will not be able to load the image inside the application window.

hen the next function is SDL_Rect nama_rect, which serves to determine the source of the image area and image area that will be displayed. For example, if you have a photo that contains 5 rows of cat with a distance of 100 pixels. Then you will choose a cat in the number two position of the photo, then you can do with SDL_Rect. This method is applied very nice, because you do not have to load 5 cat photos.

Of course, a game must be controlled by using keyboar, mouse, joystick or the like. Then each motion to be controlled should be entered in the main program loop. We will study it later.

Remember, every movement resulting in a SDL program is a movement per frame. That is, the old frame is removed and then replaced with a new frame. SDL_FillRect (screen, 0, SDL_MapRGB (screen-> format, 0, 0, 0)) a duty to clean up the old frame. After that, the picture that you have charge the last will be displayed in a new frame with the function SDL_BlitSurface (bmp, 0, screen, & dstrect). Of course, this new frame should be updated by your computer memory. You simply give the command SDL_Flip (screen).

Remember also, that you previously only clear picture of the application window, instead of the computer memory. So you must remove the old frame memory of your computer. So the new frame will have space in your computer memory. The function is SDL_FreeSurface(bmp).

Finish, this is the first learn in basic SDL. Don't worry, you will get understand if you always doing practice. So, please come again. We would like to give you this tutorial for free.

Post a Comment