Implementing the bitmap loaderGo ahead and create Bitmap.h and Bitmap.c inside your src folder.
As usual, do not forget to declare the .c source file in the Makefile:
Below are the code snippets of Bitmap.h and Bitmap.c. You should do a little research and make sure to know exactly what is going on, rather than just copy/pasting. LCOM teachers will not tolerate it.
You should now compile the program to make sure there are no errors related to what we have just introduced to the project.
Here is a quick explanation about the Bitmap functions: loadBitmap, drawBitmap and deleteBitmap.
loadBitmapThis function receives a path to an image as a string and loads that image to memory, it then returns a pointer to the memory region where the image was loaded.
drawBitmapThis function receives a bitmap pointer, a coordinate and an alignment.
If the alignment is ALIGN_LEFT, the bitmap is drawn with its top left corner at the specified coordinates.
If the alignment is ALIGN_CENTER, the bitmap is drawn with the middle of the top border at the specified coordinates.
If the alignment is ALIGN_RIGHT, the bitmap is drawn with its top right corner at the specified coordinates.
deleteBitmapThis function receives a pointer to a bitmap and frees the memory used by it, destroying the bitmap.
Preparing an imageSince we are using a 16-bit graphics mode, we can not load every image type. In fact, the load function in Bitmap.c is only able to load .bmp images which are saved in 5:6:5 mode.
In order to use images in our project we will need to prepare them by opening them with an editor and save them in this specific format. I am going to use GIMP to edit the images.
Let's add an image background to our program. Since we are using a graphics mode with a resolution of 800x600, I am going to use this image.
Go ahead and open the image with GIMP. Now select File > Export and name it test.bmp. An export dialog will pop up; expand the Advanced Options and under 16 bits select R5 G6 B5 and finally press Export.
This image is now prepared to be correctly loaded by our program, so let's add it. Inside the res folder, create a new folder called images. Paste the exported image - test.bmp - inside the newly created images folder.
If you open test.bmp you will notice the quality has decreased. That has to due with the fact we exported it as a 16 bit image, since that is the only mode supported by our program. There's nothing to do about it.
Do not forget to repeat the same exporting process every time you want to add a new image to the project. You first need to export it as a 16-bit 5:6:5 .bmp image.
Loading the new backgroundOpen FlappyNix.h and include Bitmap.h. Also, add a Bitmap* called test to the FlappyNix struct.
Open FlappyNix.c and load the bitmap as shown in line 24.
After that, go to the draw method and instead of filling the screen - remove that line, draw the loaded bitmap - add line 87. Your draw method should now look as follows:
We are now ready to see the final result! But first, do not forget to compile and since we have edited the res folder, you will also need to run the install script again. Always remember, if the res folder is modified, rerun sh install.sh.
Now you are ready to run the program. It should look something like the image below. Isn't that awesome?