Adding the timerGo ahead and add the timer implementation you have developed during class as well as i8254.h to the src folder. Do not forget to declare it in the Makefile.
Now we need to declare that FlappyNix has a timer. I have added lines 3, 7 and 11 to FlappyNix.h:
We will have to modify the methods of FlappyNix.
To start, I have added line 8: a const int represnting the mouse update FPS multiplier. What is that? It is an integer that multiplied by the FPS will determine the refresh rate of the mouse. Since FPS = 25 and mouseFPSmult = 3, the mouse position will be tracked at 25 * 3 = 75 FPS.
Regarding the start function, I have added lines 15, 18 and 23, which are responsible for subscribing timer interruptions, resetting the timer frequency and creating a new timer, respectively.
Regarding the update function, I have added line 32, which at every update, before registering any interruption, resets the timer tick flag.
Afterwards, from line 44 to 46, I check for a timer interruption and if there is one, I activate the timer->ticked flag and increment the timer counter - yes, that is exactly what the timerHandler() function does:
I have also added lines 58 to 67. Line 60 is where the mouse draw function call will be placed when we implement the mouse - 75 FPS. Inside this block, there is another block (lines 62 to 66) which is executed at 25 FPS. This block is where we are going to update our moving rectangle, but we will get there... Let's focus on planning the program's structure first.
Moving on, I have not modified the draw function. I did modify the stop function though: I have added a call to unsubscribe the timer and to delete it afterwards, as you can see below.
Now is a good time to confirm the program is still able to run without any problems. Compile and run it. So far, although we have added a couple of lines, the program should do exactly the same: show a blue screen and terminate when the Esc key is pressed.
Is it still working? Good! Let's move on.
Making a rectangle moveLet's do some interesting stuff: draw a rectangle and make it move horizontally to the right. In order to do this, we will need a variable to save the location of the rectangle - line 13.
Now we need to initialize this variable! Let's make the rectangle start at x = 10 (line 21).
Let's edit the update function and make the x location of the rectangle increment every time - line 67.
Finally, let's draw a rectangle that starts at flappy->tempRecX with constant width and height of 200 pixels - line 77.
Side note: my drawFilledRectangle() function might be different from yours. If you don't understand mine, here is a quick explanation: it receives five arguments - the first two arguments are the x and y coordinates of the top left corner of the rectangle; the second two arguments are the x and y coordinates of the bottom right corner of the rectangle; the last argument is the color of the rectangle.
And that is it! Compile and run your program and you should see a rectangle moving to the right at constant speed! You should still be able to exit the program by pressing the Esc key on the keyboard. Pretty nice, right?