Single Pixel

In this tutorial we're going to start simple, I mean really simple. We are going to draw a single red pixel in the upper left corner of the screen step-by-step. Creating something as simple as a single pixel can be more difficult than you might think, so let's get started.


Project Setup


Create main.cpp

Assuming you now have your project set up, the first thing we're going to do is create a file called main.cpp and stick the the following code into it. You need to place this into the source directory and not the include directory. There's really only 4 lines of code to worry about, but it probably all looks like gibberish to you. Well, the first 2 lines should look like gibberish, but the last 2 lines should look familiar if you've programmed in C or C++ before.

The while(1); line just means to loop forever. You actually don't need it in this particular program for it to work. I always just include it by default since it's basically the game loop. When we start working on more complicated projects, then we'll put stuff in that loop, but for now it's empty.

The return 0 line is standard in C and C++. Notice that our main function has to return an integer when the program finishes. If some error occurred in the program and it has to terminate early then some integer will be returned that corresponds to some error code. If it makes it to this line here, then we can assume that everything ran fine and we can return 0, meaning that no errors have occurred.

Now for the two gibberish lines, I'll explain below.

main.cpp

                                        
int main()
{
    *(volatile unsigned short*)0x04000000 = 0x0403;   
    *(volatile unsigned short*)0x06000000 = 0x001F;
    while(1);
    return 0;
}                
                        
                    

First Line Explanation


Second Line Explanation


Run and Finish

Now that all of that is explained, all we have left to do is compile and run our little program. Please make sure you have your project set up and structured as explained in the Project Setup section and that you're Makefile is in place. Once you've done that go ahead and compile the program as explained in the Setup section. Now open up your Gameboy Advance emulator and navigate to your project folder and open up the file with the .gba extension. What you should see is the image on the right. You may not be able to see it so I would suggest making the window bigger. You can set the window size to x4 in the Options -> Video menu.

If you don't see the red pixel then try going through everything again and see if there was anything you missed.

VisualBoy Advance