Learn More

Monday, August 8, 2011

Loading Images From Another Extention

game threelas logo
Warmest regards all my friends. Allow us to explain the next steps in making a game of two-dimensional. We hope that you have read our post about How To Install the SDL_image In CodeBlocks and How To Install SDL In CodeBlocks. Before discussing further, we want you to download the support files needed for our game project. After that, please extract all files. Create a project named Game1. Put the files background.png, sprites.png, main menu.png, in Game1 folder.

Gather all the required libraries

In this case we will load the three files with the extension png. Of course this can not be done without SDL_image. Therefore, our first step is to involve SDL_image.h so we can run the game.


Create new window

If you see, the background of the game we are measuring 320 x 240 pixels. Surely we have to reorder the dimensions of our game window with the same size of the background, so we'll get better results.


Load images

This time we will load the three images with png extensions. By involving SDL_image, then we can load those images. Here we use IMG_Load command to load an image other than bmp.


Locate background on screen

After we load it we certainly need to adjust the position of the image on the screen. However, in this tutorial, we focus on the set the display background. To make it easier to understand. We must be able to put right in the middle of the screen background. Therefore we need the formula. Namely the width of the screen minus the width of the background and then divided by two. And the screen height minus the height of the background divided by two. And laying order on the screen background is to use SDL_Rect.


Draw background

The next step is to draw the background on the screen. We can do this by giving the command SDL_BlitSurface. However before, the screens must be cleaned from the old images . The command to clear this is SDL_FillRect.


Free loaded png

Of course, the files that are loaded will be stored in computer memory. Of course, after not being used, we must remove the image from your computer. With SDL_FreeSurface then we can remove the images loaded from the computer memory.



Ok, this time with our tutorial is complete. Follow along with our tutorial, step by step so you can easily understand.

  1. Is it possible to get a new download link for the support files? The link is dead :(

    ReplyDelete
  2. thanks for your notify. I will try to make the new one soon.

    ReplyDelete