GBA development simple start

zhaozj2021-02-08  234

GBA development is simple to get started by xkinhttp: //airgl.myrice.com

I. GBA Development Pack - Devkitadv Profile Devkitadv mainly includes two parts, one is the GCC compiler, the second is the GBA library. GCC compiler features and our common VCs are similar, but there is less editing source code text editor ( At least I didn't find it, I use Editplus, UltraEdit can also be), and it is not supported (Class), it is really a headache, only Struct is replaced. It is to write our code to compile us. Two-binary executable, of course, this executable is relative to the GBA and GBA simulator. It's like the exe file in Windows that cannot be used on the MAC machine; the GBA library provides image, control and sound one Series functions, and GCC can be used. Downloads: http://occultForces.mine.nu/~darkfader/gba/files/devkitadv.zip II. DEVKITADV installation is not good, you can use it directly, compile When setting the devkitadv's path, it is recommended to make a batch file, such as be. Bat set path = d: / devkitadv / bin;% Path% CMD (Win98 is Command). The simplest GBA program (T1) / / main.c

// Some basic data types

Typedef unsigned char u8;

Typedef unsigned short u16;

Typedef unsigned long u32;

#define reg_dispcnt * (u16 *) 0x04000000 // Display register address

#define vram 0x06000000 // Image buffer address

#define m5_vram 0x0600A000 // M5 buffer address

#define backbuffer 0x010 // Double buffer / back buffer address

#define palette 0x5000000 // Toner Address

#define mode_3 0x03 // 240 * 160 15bits / single buffer

#define mode_4 0x04 // 240 * 160 8BITS / double buffer

#define mode_5 0x05 // 160 * 128 15BITS / double buffer

#define bg2_enable 0x0400 // BG_2

#define setmode (mode) reg_dispcnt = (mode) // Set the macro definition of the display mode

// ----------- Main program ------------

int main ()

{

// Set screen mode, use mode_4

SetMode (MODE_4 | BG2_ENABLE);

} 1.Mode_5 and MODE_3 are all 16bits, but Mode_3 only has a single buffer, and the animation effect is definitely no double buffer, so it is excluded, thus excludes Mode_3; 2.Mode_4 is 8bits, theoretical 256 colors for the handheld, although 16bits true color The temptation no one wants to resist, but Mode_5 only 160 * 128, in practice, it is recommended to use mode_4. It is very simple - it is true, now you have to compile it with GCC -Lm -o main.elf main. C Objcopy -V -O binary main.elf main.bin You will see more "main.bin" under the directory, this is the binary file that can be performed on the GBA simulator! T1-T7 directory is the source directory There is a make.bat inside, and the code can be used to compile it after modifying the code, but I have to pay attention to my devkitadv is installed in D:, if you have to change the Path parameter of Make.bat. IV. The GBA program (T2) in the MODE_4 background layer draws in the MODE_4 in the GBA, one map is to pass through: 1. Convert the original 256 color image file to * .h / * .c data file, we use It is , here as examples "image.bmp", we got a "image.h" file after conversion; 2. At the beginning of the program #include "image.h", you can use it in the program " Image.h "Demolition board and image data; 3. Write the palette and image data defined by" image.h "in the program to the palette and image buffer of the MODE_4 background layer. In addition, GBA is also With the object layer set for the elves, its usage and background layer is the same, but the function is a bit different. The address is 0x06000000. The address is not detailed in detail, everyone can directly output the wizard data directly to the object buffer. The following is the source program: ... // contains the header file of the image palette and the data.

#include "gfx / image.h"

/ / ----------- Global variable --------

// System palette

U16 * Palette_mem = (u16 *) Palette;

// Image buffer

U16 * video_buffer = (u16 *) VRAM;

// ----------- Function definition ---------

// Mode_4 drawing function

Void Draw (U16 * SRC_PALETTE, U16 * SRC_DATA, U16 * DST_PALETTE, U16 * DST_DATA);

// ----------- Main program ------------

int main ()

{

// Set screen mode, use mode_4

SetMode (MODE_4 | BG2_ENABLE);

// In the background layer drawing, Palette and Data are the number of palette and image data defined in "image.h".

Draw (Palette, Data, Palette_Mem, Video_Buffer;

}

// Mode_4 drawing function

Void Draw (U16 * SRC_PALETTE, U16 * SRC_DATA, U16 * DST_PALETTE, U16 * DST_DATA)

{

Int loop, x, y;

// Write a palette

For (loop = 0; loop <256; loop )

DST_PALETTE [loop] = src_palette [loop];

// Write an image buffer

FOR (x = 0; x <120; x )

{

FOR (y = 0; y <160; y )

{

DST_DATA [(Y) * 120 (x)] = SRC_DATA [(Y) * 120 (x)];

}

}

} After compiling, get main.bin, then run in the GBA simulator, you can get this result: everyone can see it is a bit disappointment ~ Yes, 256 colors use PPMM really ... Next, we will Use mode_5 to paint 16BITS true mm! 5. The GBA program (T3) drawing in MODE_5 is painted in the MODE_5 in GBA. It is also similar to three steps, but does not need palette data: 1. The original realistic image file is converted into * .h / * .C data file, we use , here "image.bmp" (240 * 160) as an example, DOS window into the targa2gba directory, input " T2g mode5 image.bmp image.h ", we got a" image.h "file after conversion; 2. At the beginning of the program #include" image.h ", you can use" image.h "definition in the program. Image data; 3. Write the image data defined by "image.h" in the program. The source program is included in the source program: // contains the header file of the image data #include "gfx / image.h"

/ / ----------- Global variable --------

// Image buffer

U16 * video_buffer = (u16 *) VRAM;

// ----------- Function definition ---------

// Mode_5 drawing function

Void Draw (int X, int y, int W, int h, u16 * src_data, u16 * dst_data);

// ----------- Main program ------------

int main ()

{

/ / Set screen mode, use mode_5

SetMode (MODE_5 | BG2_ENABLE);

// Drawing on the background layer, Image is an image data defined in "image.h"

DRAW (0, 0, 240, 160, Image, Video_Buffer);

}

// Mode_5 drawing function

Void Draw (int X, int y, int W, int h, u16 * src_data, u16 * dst_data)

{

INT I, O, IDST;

/ / Copy the source image data to the designated place of the image buffer

IDST = (Y * 160) X;

For (i = 0; i

{

For (o = 0; o

{

IF (* SRC_DATA! = 0)

{

DST_DATA [IDST] = * SRC_DATA;

}

IDST ;

SRC_DATA ;

}

IDST = (160-W);

}

}

After compiling, running results: Oh, isn't it great? This is the result of 24bits "image.bmp" (240 * 160) under the display results under MODE_5 16BITS, the image of 240 * 160 to 160 * 128 under MODE_5, which is Mode_5 Full screen display screen is only so big. Six. The Mode_5 GBA program (T4) displayed in full screen (T4) Since the GBA does not support linear image transformation, the resulting result will generate some mosaic phenomenon, or now this transformation function and the final result, In fact, the quality is still acceptable. You can try this new MODE_5. // to switch to the new MODE_5 full screen mode, the principle is to exchange the display register data x, y, get 128 * 160 display, GBA It will be displayed in full screen.

Void setflipmode (int page)

{

U16 * ioreg = (u16 *) 0x4000000;

* ioreg = 5 ((Page & 1) >> 4) (1 >> 10);

ioreg [0x10] = 0;

IOREG [0x11] = 256;

IOREG [0x12] = 128; ioreg [0x13] = 0;

}

int main ()

{

/ / Set screen mode, use mode_5

SetMode (MODE_5 | BG2_ENABLE);

// switch mode

Setflipmode (0);

// Drawing on the background layer, Image is an image data defined in "image.h"

DRAW (0, 0, 240, 160, Image, Video_Buffer);

Return (0);

}

Come ON to see the result: At this time, the full screen mode under the new MODE_5 is flipped and amplified, because the resolution of Model_5 is 160 * 128, and the above program is drawn is a 240 * 160 image, so some I can't see it. If you don't display our images like MODEL_4, then only the original image file is done, you are the step of processing the image file under the new model_5: 1. Turn the original image clockwise 90 degrees 2. 2. Return the image level; 3. Adjust the image size to 160 * 128. Here is a schematic:

Original image

1

2

3

OK, you can get the image after step 3 into bin. You can get: You can see a slight mosaic, but it is better than 256 colors. What model is used to see the type of game, if it is some Classic RPG / ACT / SLG, etc., is recommended to use MODE_4. VI. GBA Double Cuff Display (T5) Everyone will find images in flashing (240 * 160 mm icons in section 6) when doing the above Model_5. Phase ...), and the model_4 is relatively stable - this is because MODEL_5 is to process 16bits (substantially 15bits) images, the amount of data is much larger than the 8BITS under Model_4, without using double buffer When the image is filled, it will cause flashing. This is why we abandoned Model_3 reasons ... The principle is also very simple, and the image is filled in the back buffer and then output it directly to the front buffer display. The program is a "waiting" Synchronization -> Switch Buffer "process: ... ...

/ / ----------- Global variable --------

// Image buffer

U16 * video_buffer = (u16 *) m5_vram;

// ----------- Function definition ---------

...

// Waiting for the buffer data synchronization

Void waitsync ();

// exchange buffer content

Void swapscreen ();

// ----------- Main program ------------

int main ()

{

/ / Set screen mode, use mode_5

SetMode (MODE_5 | BG2_ENABLE);

While (1)

{

// Drawing on the background layer, Image is an image data defined in "image.h"

DRAW (0, 0, 240, 160, Image, Video_Buffer);

Waitsync ();

Swapscreen ();

}

}

// Waiting for the buffer data synchronization

Void WaitSync ()

{

While (* (Volatile U16 *) 0x4000006 <160) {};

}

// exchange buffer

Void swapscreen ()

{

IF (reg_dispcnt & backbuffer)

{

REG_DISPCNT & = ~ Backbuffer;

Video_buffer = (u16 *) m5_vram;

}

Else

{

REG_DISPCNT | = Backbuffer;

Video_buffer = (u16 *) VRAM;

}

}

Eight. GBA button input (T6) tells the old half-day image, although it is opposite to MM, but everyone must have a little annoying, we now change the direction to see GBA control. ... ... ...

// button control

#define key_a 1

#define key_b 2

#define key_select 4

#DEFINE KEY_START 8

#define key_right 16 # Define Key_LEFT 32

#define key_up 64

#define key_down 128

#define key_r 256

#define key_l 512

Volatile U32 * Keys = (Volatile U32 *) 0x04000130;

// Contains the header file of the image palette and the data

#include "gfx / image.h"

/ / ----------- Global variable --------

// Image buffer

U16 * video_buffer = (u16 *) m5_vram;

// Image display coordinates

INT IMG_X, IMG_Y;

// ----------- Function definition ---------

// button control

Void keyaction ();

...

// ----------- Main program ------------

int main ()

{

/ / Set screen mode, use mode_5

SetMode (MODE_5 | BG2_ENABLE);

While (1)

{

// Process button event

KeyAction ();

// Drawing on the background layer, Image is an image data defined in "image.h"

Draw (IMG_X, IMG_Y, 96, 64, Image, Video_Buffer);

Waitsync ();

Swapscreen ();

}

}

// Process button event

void keyaction ()

{

//

IF (! (* Keys) & key_up))

{

IMG_Y = 5;

}

// Under the direction button

IF (! (* keys) & key_down)))

{

IMG_Y- = 5;

}

}

Simple, this example responds to the up and down button to control the image up and down. We didn't clear the back buffer, so that you can see the image movement, the image in the original position is still back, this can be Clearly see the work process of the double buffer. What? It's too ugly, that's good, add a background, it is OK ... After one is alpha. #Include "bg.h" .... .. // Popular draw background DRAW (0,0,160,128, bg, video_buffer); // Draw MMDRAW (IMG_X, IMG_Y, 96, 64, Image, Video_Buffer); ... 9. Simple Sound Output (T7 Simple is the best, here we use a ready-made sound module (Troff Player, by vova & serge). Here you need to use a conversion tool to convert MOD music files into GBA * .c / * .h sound data files. MOD and MIDI are similar, but support more stronger results. // mod data file

#include "song_data.h"

// MOD play function file

#include "modplayer.h"

// ----------- Main program ------------

int main ()

{

// Set screen mode, use mode_4

SetMode (MODE_4 | BG2_ENABLE);

// Initialize the sound (number, volume)

INITSOUND (2, 7);

// Initialize music (beat, loop)

INITSONG (20000, 0);

While (1)

{

/ / Update the music playback status

Updatesong ();

}

}

OK, so EZ. 10. Establish scrollab / zoom / rotate background (T8) This section is mainly due to the comment in the source program, which is not described in detail. "GBA.H" contains basic macro Definition, "maths.h" is the value of SIN / COS multiply 256, "main.h" includes a function we define background structure and operational background. The map background in the program is constructed from different pieces, and These tiles are unified next to an image file, so that each of the tiles will have an index number; the map information simply records how many units in this map (Figure) and each unit corresponding to each unit The number is OK, "gfx / tiles.h" in the example is the base camp, and "GFX / Level1.h" is a list of maps index. The map tool is "Map Editor Beta 4". Scroll / Zoom / Rotation is made through a series of simple mathematical calculations, modify some background properties provided by the GBA system to complete because it is made by hardware (MODE_1), so speed is very fast, I also have a direct modification of pixels under Mode_5 Point location to complete the routine of the rotation, you can compare it later. 11. The background rotation under MODE_5 (T9) This example is completed by calculating the new location of the pixel after the rotation, the principle is simple, can be applied directly Elf. / ----------- Main program ----------- int main ()

{

// angle

INT i = 0;

/ / Set screen mode, use mode_5

SetMode (MODE_5 | BG2_ENABLE);

While (1)

{

// Drawing on the background layer, Image is an image data defined in "image.h"

DRAW (0, 0, 128, 128, BG, VIDEO_BUFFER);

// Write the image data buffer after the rotation

RotateBG (BG, Video_Buffer, i );

IF (i == 360) i = 0;

}

}

// Rotating background

Void RotateBG (U16 * SRC, U16 * DST, INT ANGLE)

{

INT X, Y, YS, YC, TX, TY;

Unsigned int srcptr = 0, DSTPTR = 0;

/ / Pointer to calculate the pixel after rotation

For (y = -80; y <80; y )

{

YS = y * sin [Angle];

YC = Y * COS [Angle];

For (x = -80; x <80; x )

{

Tx = ((((x * cos [angle]) - ys) >> 7;

TY = (((x * sin [angle]) YC) >> 7;

SRCPTR = (((Ty << 7) TX (160 * 80 80)) & 16383);

DST [DSTPTR] = SRC [srcptr];

DSTPTR ;

}

}

}

Twelve. The Chinese under GBA show huh, the English has not been able to write to the Chinese, and the program core technology is from the Chinese display technology under DOS. In fact and the foreigner Showing 26 English alphabetical principles, we use 16 * 16-point box for complete Chinese display. Everyone must know that there is no use of location (internal code) input method in the input method, this input method uses 4 numbers To complete the Chinese input ---- Estimate no one will use it to chat with MM. The Chinese word library can also be seen as a big bitmap, the above is full of common Chinese characters, and location (internal code) 4 numbers of the input method represent a diarrhea in the area and specific location in the font. For example, the location code of "said" is "4321", when we choose location input and enter "4321 in Notepad" 4321 ", The input method will immediately go to the 4321 area in the word, and then display the 4321 area of ​​the data transfer to the system, so" say "is coming out. The word display in GBA is the same principle. However, since the GBA has no file input / output function, we have to convert the binary dot matrix into array storage data, including location code and dot matrix (bitmap). This is not described in detail, I am for everyone Do a conversion tool ---- "HZK2GBA" (Han Character -> GBA). Attach the source program ^ _ ^. You first have to make all your text into a text file, such as "zk.txt", then Execute "HZK2GBA ZK.TXT" to get "gbazk.h" file, this is the font file file file file available for GBA. The purpose of this is to send all the Chinese characters in the font to GBA, leaving only the Chinese characters we used. It can save space and memory. The next job is to use the "Gbazk.h" to write down in GBA. ... # define RGB (R, G, B) ( (r) (g << 5) (b << 10)) // Set the color of GBA

#define max 10240 // Maximum number of characters

// Contains the header file of Chinese letter library data

#include "gfx / gbazk.h"

/ / ----------- Global variable --------

// Image buffer

U16 * video_buffer = (u16 *) VRAM;

// ----------- Main program ------------

int main ()

{

/ / Set screen mode, use mode_5

SetMode (MODE_5 | BG2_ENABLE);

// Write character

DrawText ("Silver Tutor", 20, 20, 255, 0, 0);

}

// - Write Chinese character functions ----------------

Void DrawText (unsigned char * str, int x, int y, int R, int G, int b)

{

INT I, J, K, N;

// Character location, bit, actual location

INT Q, Wei, Location;

While (* STR)

{

// Get a single word code code

QU = * (STR ) - 0xA0;

WEI = * (STR ) - 0xA0;

Location = qu * 94 wei;

/ / Find in the font

FOR (n = 0; n

{

IF (zkqw [n] == location)

{

/ / Draw characters according to character data

For (i = 0; i <16; i )

For (j = 0; j <2; j )

FOR (k = 0; k <8; k )

IF (Convert (ZKData [N * 32 i * 2 J], 7-K))

Drawpixel (X J * 8 K, Y I, R, G, B);

X = 16;

}

}

}

}

INT Convert (U16 Str, INT N) {

Return ((STR >> N) & 0x1);

}

// - Draw point function ---------------------

Void Drawpixel (int X, int y, int r, int g, int b)

{

Video_buffer [x y * 160] = RGB (R, G, B);

}

It's so much, the problem of font styles is waiting for the next to solve the TrueType font display, the font size, you can use the background / object layer to scale or directly zoom the pixel method. As for English display ~~~ ~~ ^ _ *, so sleepy ~~~~ Next !!!!! Let's first let a paragraph, you can get more things on www.gbadev.com, I will not be cumbersome here. .

转载请注明原文地址:https://www.9cbs.com/read-2523.html

New Post(0)