The first step is to always setup your project. The link provided here will show you step-by-step how to do that. If you've already downloaded Devkitpro, then you can skip that step and focus on setting up the folder structure. This is something you'll do for every project so in later projects I'll just assume you've completed this step.
Random Pixels
If you know how to draw a single pixel onto the screen, then you really know how to draw any number of pixels on to the screen. For this tutorial we're just going to create pixels and give them a random color and place them in a random position on the screen.
Project Setup
Initial Code
If you followed from the previous tutorial, then most of the code is already in place. Go ahead and copy this code into the main.cpp file and save it in the source folder. Make sure you can compile and run this just fine before moving on. If it doesn't work here and you go and modify it then it just makes it harder to find the bugs.
main.cpp
#define u16 unsigned short
#define vu16 volatile u16
#define REG_DISP *(vu16*)0x04000000
#define VRAM (vu16*)0x06000000
u16 color15(u16 r, u16 g, u16 b);
const int WIDTH = 240;
const int HEIGHT = 160;
int x = 120;
int y = 80;
int main()
{
REG_DISP = 0x0403;
vu16 *vram = VRAM;
vram[x + WIDTH * y] = color15(31, 0, 0);
//*(volatile unsigned short*)0x04000000 = 0x0403;
//*(volatile unsigned short*)0x06000000 = 0x001F;
while(1);
return 0;
}
u16 color15(u16 r, u16 g, u16 b)
{
return r | g << 5 | b << 10;
}
Include the Standard Library
Since we're going to be calling a random function we need to include the package that contains that function. That package is called stdlib.h which stands for standard library.
main.cpp
#include <stdlib.h>
Include the Standard Library
We're going to use the rand() function that the standard library provides in order to generate random values so we can set a random position and a random color. We'll create two new functions called randomColor() and randomPosition(). The formula rand() % num will generate a random value between 0 and the value of the number -1. So, rand() % 32 will generate a random value between 0 and 31. So in our functions we will use this to generate random values for the red, green, and blue 15 bit colors, and also for the x and y positions. We then comment out our previous call to vram and call these functions instead. We also want to put it inside of the while loop.
Don't forget to declare the new functions at the start of the program!
main.cpp
#include <stdlib.h>
#define u16 unsigned short
#define vu16 volatile u16
#define REG_DISP *(vu16*)0x04000000
#define VRAM (vu16*)0x06000000
u16 color15(u16 r, u16 g, u16 b);
u16 randomColor();
u16 randomPosition();
const int WIDTH = 240;
const int HEIGHT = 160;
//int x = 120;
//int y = 80;
int main()
{
REG_DISP = 0x0403;
vu16 *vram = VRAM;
//vram[x + WIDTH * y] = color15(31, 0, 0);
while(1) {
vram[randomPosition()] = randomColor();
}
return 0;
}
u16 color15(u16 r, u16 g, u16 b)
{
return r | g << 5 | b << 10;
}
u16 randomColor()
{
u16 r = rand() % 32;
u16 g = rand() % 32;
u16 b = rand() % 32;
return color15(r, g, b);
}
u16 randomPosition()
{
u16 x = rand() % WIDTH;
u16 y = rand() % HEIGHT;
return x + WIDTH * y;
}
Run and Finish
Compiling and running the new code will now continually create a new color and location which should quickly fill up the screen with a bunch of colored pixels. This will run forever, so enjoy it and then stop it whenever you want.
