Creating the game stateStart by creating GameState.h and GameState.c - do not forget to declare it in the Makefile.
Let's start with small goals, ok? First let's try to put the background scrolling correctly through the screen.
Download this background, export it as a .bmp using R5 G6 B5 with GIMP and place it inside your res/images folder.
The game state should have four methods that every state should have - new, update, draw and delete. Our game state object, for now, should have an integer telling if the state is done/finished, a bitmap pointer to the background and an integer corresponding to the background horizontal position - because the background is going to be moving to the left. Knowing this, try to code GameState.h for yourself. Afterwards, compare it to the my GameState.h below.
Now open GameState.c and try to code everything for yourself - come on, you can do it!
I'll give a little help: in the initialization you have to initialize the background position and actually load the background image; in the update method, you have to update the background position, to make the background slowly move left, and when it has moved enough, reset the position to zero; in the draw method, you just need to draw the background at the right position; and finally in the delete method just delete the background image and free the state.
Easy, right? You should end up having something like this:
Ok, so now we have the game state ready, we just need to add it to every mechanism (function) of our state machine.
Add a new state identifier:
In FlappyNix.c, include the state header file:
Add the state update and draw method calls to each switch case:
Update the change state function as well:
Also update check if state is done and delete current state methods. Notice that I've slightly modified the switch case for the main menu state as well.
Quick previewIf we have done everything correctly so far, when we run the program and press play, we should see the background infinitely scrolling to the left. If we press the ESC key on the keyboard, we should be able to return to the main menu as well. So go ahead, compile, install and run your program. It should look something like this:
Adding the groundNow that we have the background moving, let's add a moving ground as well. The ground will be moving faster than the background to create a sense of perspective.
Download the ground image, edit it like you have done before and place it inside the res/images folder.
Just like what we did to add the background image, do the same to add the ground image: add a bitmap pointer for the actual image as well as an integer to hold it's horizontal position. After that, initialize them just like you did with the background image:
After that, you just need to update the ground position - I created a separated function for that; draw the ground - make sure you draw it after the background, otherwise it won't show up; add the delete ground bitmap statement in the state delete method. Here's what all of this should look like: