Adding keyboard and KBC source codeDuring class, you should have developed functions to interact with the keyboard using the kbc. Now it is time to add these to our src folder and add them to the Makefile SRCS tag - I usually declare them by alphabetical order. Below is a screenshot with the changes I have made so far.
Introducing objects in C
As you might know, C++ is object-oriented, but C is not. But there is a way to make C programs resemble some object-oriented-like programming language. I think it is time for us to start implementing this in our project.
Let's create our main object, the engine of our application: flappy-nix. Create both FlappyNix.c and FlappyNix.h inside src.
After that, what should our "object" FlappyNix contain and represent?
It will have four methods any object should always have:
It should also have three variables:
- done - tells if the program is done/is going to terminate;
- draw - a flag that tells the program needs to be redrawn;
- scancode - whenever a keyboard key is pressed, this will contain the code of that key.
There will also be another variable: IRQ_SET_KB, which will be used to detect keyboard interruptions.
Here is what my FlappyNix.h looks like so far:
Now let's actually implement these methods in FlappyNix.c.
Side note: notice I have declared a global const int FPS, which is the Frames Per Second at which the application will be redrawn once we implement the timer. For now, don't worry about it.
Regarding startFlappyNix(), this function returns a pointer to a FlappyNix "object".
First I allocate space for a new FlappyNix.
After that, I subscribe the keyboard to activate keyboard interruptions.
Then, I initialize the object variables.
Finally, I return the pointer to the newly created FlappyNix.
This function should be familiar to you from the LCOM lectures. I'm not going to go into much detail about it: basically, we are checking for hardware interruptions. At the moment we are only registering keyboard interruptions and the code of the pressed key associated to that interruption (lines 32-34). Later we'll be adding code to this function in order to receive timer and mouse interruptions.
Afterwards, we check the scancode, and if it is different than zero, it means a key was pressed. We check if that key was the Esc key. If it was, we tell that the program is done! Easy, right?
Notice that, just like color names, I also have some key names - line 42 - because that way I don't have to remember every key code or check a table of codes, I just use the key name. As you can see below, I declared these names in Keyboard.h. You can use the snippet below the screenshot in your own projects, it contains every possible key of the keyboard and the respective key code.
Note: the KEY_UP macro presented in the picture above should make use of '&' instead of '|'.
This one is pretty straight forward: we fill the display with the color blue.
Here we unsubscribe the keyboard interruptions and free the memory allocated to the FlappyNix "object".
Updating the Makefile
Do not forget to update the Makefile each time you add a new source file!
I added a new line - line 8 - where, from now on, I will declare the source code of "objects" we create.
Improving main.cNow comes the fun part: let's make use of everything we have been working on and see the results. Go to main.c and edit it according to the screen below.
First, declare and create a new FlappyNix object.
Then it is really simple: while flappy is not done do the following indefinitely:
- Update flappy;
- If flappy is not done and draw flag is active - draw flappy.
Below is a snippet with the current content of main.c.
Testing our project so farIt's time to test our program! Go to minix, browse to the project folder, compile and run the project.
If everything went well, when you run the program, a blue screen should appear and should only go away when you press the Esc key on the keyboard. And that terminates the program.