Below I will explain the above procedures one by one
The first three lines:
Typedef unsigned char u8;
Typedef unsigned short u16;
Typedef unsigned long u32;
Define your own data type of your GBA program, because so we can easily know the number of bytes per data in GBA.
#define reg_dispcnt * (u16 *) 0x04000000 // Display register address
REG_DISPCNT is a register setting display mode. There is a Mode0-mode5 six display mode in GBA. Our program's display mode is Mode4, which is a dual buffer display mode of 240x160 (256 colors). For example, Mode3 is 240x160 16-bit true colored single buffer display mode.
#define vram 0x06000000 // Image buffer address
#define palette 0x5000000 // Toner Address
VRAM is the display memory of GBA, simply saying data in the VRAM is an image on the screen. Like our usual computer. Palette is a palette address, simply, is a place where the image palette is installed.
#define mode_4 0x04 // mode4 logo
#define bg2_enable 0x0400 // bg_2 flag
Mode_4 and bg2_enable are values to set REG_DISPCNT.
Let's take a look at what the main function is mainly done.
The first is setmode (MODE_4 | BG2_ENABLE); set the display mode to MODE_4. Then the Draw function is executed, and the image data and palette data of our HelloWorld.h are then copied to the specified memory. In the Draw this function It is all used to copy the palette data and image data to the memory using the FOR loop.
As mentioned earlier, the MODE4 in GBA supports 240 * 160 and 256 color display, then displaying a one in the memory VRAM to correspond to a point on the screen. Come, the entire 240 * 160 size screen There should be 240 * 160 = 38400 bytes. So we have a 38400/2 = 19200 elements in HelloWorld.h (a U16 is two bytes). And we can also calculate the screen by formula One point corresponds to the address of the VRAM. This can get the following write point function. In particular, the access to the VRAM can only be the U16 size data type, and cannot be U8 (including char). And a U16 type The data represents the information of two points, and the lower 8 bit corresponds to the front point, and the high 8 bit corresponds to the back point. So we need some algorithm to implement a single point in the screen, please see the following Write point function
Void SetPixel (Int x, int y, u8 color)
{
Register U16 * TC;
Tc = video_buffer y * 120 x / 2;
IF (x & 1) // If x is odd
* TC = ((* TC & 0x00FF) (Color << 8));
Else
* TC = (* TC & 0xFF00) Color;
}
The video_buffer is the address of the memory VRAM. The value of the point and the VRAM on the screen is the first byte of the first byte of the VRAM, which is the color in the screen (0, 0). Color in color The reference on the board, the second byte is the color information of the screen (1, 0), the third byte is the color information of (2, 0), a class push. To the 241-byte is the screen (0 1) color information. Then, 242 is (1, 1) information ... This relationship, we can find the formula, information on the screen (x, y), should be stored in the VRAM * 240 X bytes, then if we want to paint (x, y), only need to modify the Y * 240 X byte in the vram is OK. However, there is a problem in this problem. With us, that is, the above black body is written. The access to VRAM in GBA cannot be a single byte of a single byte, but must be accessed by 16 bilees. This is to say that you write every time you write Will affect the information next to the point next to it. Fortunately, we can complete the write point of (x, y) by simultaneous operations, and the code is already above while avoiding the effects. The code is already above. Because * TC It is a 16-bit pointer, so TC plus 1 is equal to the distance of 16-bit two bytes. So in the code, tc = video_buffer y * 120 x / 2 instead of the tc = video_buffer y * 240 x. As for the following code, I don't talk much. If you feel that there is a problem, then you should check the C language book. Through this function, you can draw a line on the screen, draw a circle, a torque type, etc. And even write Chinese characters.
4. More information and information
The first program on the GBA is made, but it is still not enough to truly do GBA development. It supports BG (Object) background, Obj (Object) wizard, etc., those are GBA The technologies commonly used in the game can be used to further use the Alpha mix, Face In / Out, Mersc, etc., which can be used by BG and OBJ. Unfortunately I can't explain these things in detail. You can go to WWW I found a very detailed GBA information I wrote on .GpGame.net website, the name is GBA exploration diary. At the same time, you can also go to www.gbadev.org to see many foreign tutorial. www.gbadev.org is a very complete GBA development website You can find a lot of foreign friends to write Demo, development tools, source code, articles, and more. Although there is no official GBA development website in China, you can go to http://www.wodenstar.com/cgdn/ BBS / INDEX.PHP Forum found a lot of friends who were already doing GBA development. Here I want to remind everyone that developing GBA programs is actually a very simple thing, difficulty is the lack of information, especially Chinese materials. Finally I Tell everyone in your contact information, if you have any questions in the GBA process, I hope I can help you. QQ: 8664220, Email: TANGL_99 @ sohu.com, msn: tangl_99@hotmail.com